简体   繁体   English

从 Xamarin.Z6450242531912981C3683CAE88A32A6 调用 Android Function

[英]Call Android Function From Xamarin.Forms

I have made an Xamarin.Android Widget with a Button and TextView.我制作了一个带有按钮和 TextView 的 Xamarin.Android 小部件。 I have an instance of product (latestProduct) declared in App.cs.我在 App.cs 中声明了一个产品实例 (latestProduct)。 A new product is assigned to latestProduct when a new product is added.添加新产品时,会将新产品分配给 latestProduct。

The user is able to save a new record by clicking a Button (BtnAdd) on a Xamarin.Forms page.用户可以通过单击 Xamarin.Forms 页面上的按钮 (BtnAdd) 来保存新记录。 I am trying to update the Widget TextView when the user clicks this Button.当用户单击此按钮时,我正在尝试更新小部件 TextView。

MyPage:我的页面:

void BtnAdd_Clicked(object sender, EventArgs e) {       
     Product product = new Product
     {           
         ProductSaveTime = DateTime.Now       
     };       
     App.Database.SaveProduct(product);   
     App.latestProduct = product; 

     DependencyService.Get<IUpdateDataService>().UpdateWidgetUI();
}  

App.cs:应用程序.cs:

public static Product latestProduct;

IUpdateDataService.cs: IUpdateDataService.cs:

public interface IUpdateDataService
{
    void UpdateWidgetUI();
}

WidgetClass.cs:小部件类.cs:

public class WidgetClass : AppWidgetProvider, IUpdateDataService
{
    public static String SaveClick = "Save Product";

    public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
        appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));
    }

    private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
    {
        var widgetView = new RemoteViews(context.PackageName, Resource.Layout.widget);
        UpdateData(widgetView);
        RegisterClicks(context, appWidgetIds, widgetView);
        return widgetView;
    }

    private void UpdateData(RemoteViews widgetView)
    {
        CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
        widgetView.SetTextViewText(Resource.Id.textView1, "Last Product: " + App.latestProduct.ProductSaveTime.ToString("g", currentCulture));
    }

    private void RegisterClicks(Context context, int[] appWidgetIds, RemoteViews widgetView)
    {
        var intent = new Intent(context, typeof(WidgetClass));
        intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
        intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, appWidgetIds);

        widgetView.SetOnClickPendingIntent(Resource.Id.buttonSave, GetPendingSelfIntent(context, SaveClick));
    }

    private PendingIntent GetPendingSelfIntent(Context context, string action)
    {
        var intent = new Intent(context, typeof(WidgetClass));
        intent.SetAction(action);
        return PendingIntent.GetBroadcast(context, 0, intent, 0);
    }

    public override void OnReceive(Context context, Intent intent)
    {
        base.OnReceive(context, intent);

        if (SaveClick.Equals(intent.Action))
        {
              Product product = new Product
              {           
                  ProductSaveTime = DateTime.Now       
              }; 

            App.latestProduct = product; 

            RemoteViews remoteViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
            UpdateData(remoteViews);
            AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
            ComponentName componentName = new ComponentName(context, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
            appWidgetManager.UpdateAppWidget(componentName, remoteViews);
        }
   }

   public void UpdateWidgetUI()
    {
        var widgetView = new RemoteViews(context.PackageName, Resource.Layout.widget);
        UpdateData(widgetView);

        AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
        ComponentName componentName = new ComponentName(context, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
        appWidgetManager.UpdateAppWidget(componentName, remoteViews);
    }
}

When BtnAdd is clicked, how can I use DependecyService to call UpdateData()?单击 BtnAdd 时,如何使用 DependecyService 调用 UpdateData()? The problem I have is 'context' does not exist in the current context in UpdateWidgetUI();我遇到的问题是 UpdateWidgetUI() 的当前上下文中不存在“上下文”;

You could make your MainActivity as a static variable.您可以将 MainActivity 作为 static 变量。

like:喜欢:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity Instance;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(savedInstanceState);
        Instance = this;
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

    }
}

then you want use the context,you could do like:然后你想使用上下文,你可以这样做:

public void UpdateWidgetUI()
{
    var widgetView = new RemoteViews(MainActivity.Instance.PackageName, Resource.Layout.widget);
    UpdateData(widgetView);

    AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(MainActivity.Instance);
    ComponentName componentName = new ComponentName(MainActivity.Instance, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
    appWidgetManager.UpdateAppWidget(componentName, remoteViews);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM