有没有办法使用 mvvm 轻量级 Xamarin 表单订阅和发送通用类型类的消息传递中心



这就是我现在这样做的方式,有没有更好的解决方案来制作此通用

MessagingCenter.Send(this, "XYZ", "XYZ" + " Recieved");
MessagingCenter.Subscribe<MyService, string>(this, "XYZ", async (sender, arg) =>
{
     //TO-DO
});

我们如何使用Xamarin表单中的消息传递中心发送消息: - 这是我在波纹管方法中使用的行: -

  MessagingCenter.Send(this, typeof(T).Name, typeof(T).Name + " Recieved");

databaseservice.cs ---我正在从" databaseservice.cs"发送此消息,因此在第一个paramenter中写下此消息,以partermenter(t)中的第二个参数(t).name中的" t"是通用类。(t是azure azure下载完成后,我正在发送消息以识别数据是从Azure服务器到达并保存在本地db中的数据,Bellow方法中的第二个Paramenter也是TypeOf(t)。名称。名称,它引用了键键和接收器的键"接收"是我传递的参数,以确定数据已接收或从商店中删除,以确定我在SQLite DB上执行的操作。

    /// <summary>
    /// Function to retrive Records from Azure Server and insert/Update those into localDB .
    /// </summary>
    /// <typeparam name="T">Table Name</typeparam>
    /// <param name="data">Records retrived from Azure Server that are being insrted to localDB.</param>
    /// <returns></returns>

        public async Task InsertDataToLocalDB<T>(List<T> data)
        {
            List<JObject> jDatas = new List<JObject>();
            for (int index = 0; index < data.Count; index++)
            {
                jDatas.Add(JObject.FromObject(data[index]));
            }
            try
            {
                await _store.UpsertAsync(typeof(T).Name, jDatas, true);  //_Store is MobileServiceSQLiteStore SQLite store
            }
            catch (Exception ww)
            {
                //throw;
            }

            MessagingCenter.Send(this, typeof(T).Name, typeof(T).Name + " Recieved");
        }

我们如何用来施加消息: -

   MessagingCenter.Subscribe<DatabaseService, string>(this, "Notification", async (sender, arg) =>
            {
               //After Receiving Notifications from server Call Refresh Command
                await RefreshCommand.ExecuteAsync();
            });

最新更新