Xamarin.Forms应用程序中BroadcastReceiver中的Application.Current.Properties

Xamarin.Forms应用程序中BroadcastReceiver中的Application.Current.Properties,xamarin.forms,xamarin.android,broadcastreceiver,Xamarin.forms,Xamarin.android,Broadcastreceiver,我在Application.Current.Properties中为我的Xamarin.Forms应用程序保存了一些首选项,并且有一个需要这些首选项的BroadcastReceiver。问题是Xamarin.Forms没有在BroadcastReceiver中初始化,所以我需要一种方法来访问原生Xamain.Android中的这些变量。可能吗 我需要一种在原生Xamain.Android中访问这些变量的方法 您可以使用DependencyService,此功能使Xamarin.Forms应用程序

我在Application.Current.Properties中为我的Xamarin.Forms应用程序保存了一些首选项,并且有一个需要这些首选项的BroadcastReceiver。问题是Xamarin.Forms没有在BroadcastReceiver中初始化,所以我需要一种方法来访问原生Xamain.Android中的这些变量。可能吗

我需要一种在原生Xamain.Android中访问这些变量的方法

您可以使用DependencyService,此功能使Xamarin.Forms应用程序能够执行本机应用程序可以执行的任何操作

以下是使用Android的Toast在共享代码中显示消息的示例:

public interface IToast
 {
    void LongAlert(string message);//the parameter could pass to the native platform
    void ShortAlert(string message);
 }
DependencyService.Get<IToast>().ShortAlert("short toast);
DependencyService.Get<IToast>().LongAlert("long toast);
1.在共享代码中创建接口:

public interface IToast
 {
    void LongAlert(string message);//the parameter could pass to the native platform
    void ShortAlert(string message);
 }
DependencyService.Get<IToast>().ShortAlert("short toast);
DependencyService.Get<IToast>().LongAlert("long toast);
2.安卓系统实施:

[assembly: Dependency(typeof(ToastAndroid))]
namespace Demo.Droid
{
  class ToastAndroid : IToast
   {
     public void LongAlert(string message)
      {
        Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long).Show();
      }            

     public void ShortAlert(string message)
      {           
        Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short).Show();
       }
   }
}
3.调用共享代码:

public interface IToast
 {
    void LongAlert(string message);//the parameter could pass to the native platform
    void ShortAlert(string message);
 }
DependencyService.Get<IToast>().ShortAlert("short toast);
DependencyService.Get<IToast>().LongAlert("long toast);

我自己找到了解决办法。
我使用Xamarin.Forms源代码编写了一个函数,该函数从表单应用程序中写入的同一文件中读取值。工作起来很有魅力。

据我所知,我认为这不可能实现,更好的选择可能是使用SQLITE。从Application.Current.properties获取属性不起作用。不过,您可以使用依赖注入来创建自己的类似解决方案,但这不是我想要的。