有没有办法订阅&使用mvvm light Xamarin Forms向泛型类发送消息传递中心

时间:2016-12-26 07:31:26

标签: xamarin xamarin.forms

这就是我现在正在做的事情,有没有更好的解决方案来制作这个通用的

MessagingCenter.Send(this, "XYZ", "XYZ" + " Recieved");

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

1 个答案:

答案 0 :(得分:0)

我们如何使用xamarin表单中的消息中心发送消息: - 这是我用过的方法: -

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

DatabaseService.cs ---我从&#34; DatabaseService.cs&#34;发送此消息因此,在第一个参数中写入此参考,在第二个参数typeof(T).Name中,其中&#34; T&#34;是通用类。(T是下载完成后的Azure表名称我发送消息以识别数据是从Azure服务器到达并保存在本地数据库中),波纹管方法中的第二个参数也是typeof(T).Name引用订阅发送方和接收方的密钥以及&#34;收到的&#34;是我传递给的参数识别从存储中接收或删除数据以识别我在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();
            });