【问题标题】:How accessing xamarin forms properties from android GcmService如何从 android GcmService 访问 xamarin 表单属性
【发布时间】:2017-06-12 16:12:01
【问题描述】:

我可以使用 Application.Current.Properties [key] 存储数据

但是当远程通知到达时如何从 Android 读取这些数据?

如果应用程序已关闭并且没有“当前”Xamarin 表单应用程序,我该如何读取/写入这些数据?

 [Service]
public class GcmService : GcmServiceBase
{
    public static string RegistrationID { get; private set; }

    public GcmService()
        : base(PushHandlerBroadcastReceiver.SENDER_IDS) { }

    protected override void OnRegistered(Context context, string registrationId)
    {
        Log.Verbose("PushHandlerBroadcastReceiver", "GCM Registered: " + registrationId);
        RegistrationID = registrationId;
    }

    protected override void OnMessage(Context context, Intent intent)
    {

        var msg = new StringBuilder();

        if (intent != null && intent.Extras != null)
        {
            foreach (var key in intent.Extras.KeySet())
                msg.AppendLine(key + "=" + intent.Extras.Get(key).ToString());
        }



        string textoNotificacion = intent.Extras.GetString("gcm.notification.body");
        var nombre = "Alumno - ";
        if (!string.IsNullOrEmpty(textoNotificacion))
        {
            //***************************************** //
            var idEstatus = "0";
            var textoEstatus = "Cambio de Estatus";

            if (textoNotificacion.Length > 2)
            {
                idEstatus = textoNotificacion.Substring(0, 1);
                textoEstatus = textoNotificacion.Substring(1);
            }


            try
            {

                var app = App.Current;                  

                if (app == null)
                {
                    Read and Save data in xamarin form properties here!!!

                }
                else
                {
                    var mp = (MainPage)app.MainPage;
                    nombre = mp.getNombre() + " - ";
                    mp.actualizarEstatus(idEstatus, textoEstatus);
                }





            }
            catch(Exception e)
            {

                createNotification("Aplication ",e.Message);
            }




            createNotification("Aplication ",   nombre +textoEstatus);
            return;
        }

        createNotification("Unknown message details", msg.ToString());
    }

【问题讨论】:

  • 这个GcmService,顾名思义,是一个服务,不管应用状态如何,都会执行OnMessage方法……这样想问题没有意义……你在考虑这种服务行为?
  • 不,我的问题很清楚。如何访问 Xamarin 表单属性中的数据。我说没有当前的应用程序,所以显然不是这样。我的问题是关于访问该数据的另一种方式。可能是路径文件或其他库

标签: xamarin.android xamarin.forms


【解决方案1】:

当您的应用关闭时,您无法从Application.Current.Properties [key] 读取数据,而是可以使用SharedPreferences 存储数据。 SharePreferences是Android中用于存储一些简单配置信息的机制,使用Map数据结构存储数据,存储为key-value对,以XML格式存储数据。

创建的存储文件保存在/data/data/<package name>/shares_prefs文件夹下。因此,即使用户关闭应用程序,SharedPreferences 中的数据也会保持不变。

Here 是关于如何在 Xamarin.Android 中使用 SharePreferences 的文档。

【讨论】:

  • 这仅适用于安卓但不适用于IOS,拥有xamarin表单如何访问xamarin表单属性?
  • 我对IOS不熟悉,可以开新帖提问。 :)
猜你喜欢
  • 2018-06-03
  • 2018-11-20
  • 2019-04-02
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多