Xamarin 消息中心多次订阅
Xamarin messaging center subscribing multiple times
我正在处理一个需要向另一个页面发送信息的页面,我正在通过消息中心执行此操作,但我注意到消息被发送一次并收到 2、4、6 次...取决于我打开订阅者页面的次数。奇怪的是我添加了一些代码以避免多次订阅。请参阅我的代码的相关部分:
发件人:
if (result.status == "ok")
{
MessagingCenter.Send<ChatCompartilhar, PassaParametro>(this, "PopUpData", new PassaParametro(){ Tipo = 3, Valor = result.msg, Extra = dataFinal });
await Navigation.PopPopupAsync();
}
订阅者:
Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>(this, "PopUpData");
MessagingCenter.Subscribe<ChatCompartilhar, PassaParametro>(this, "PopUpData", async (sender, arg) =>
{
Task.WaitAll(tasks.ToArray());
switch (arg.Tipo)
{
case 2: tasks.Add(Task.Run(() => ShareMap(arg))); break;
case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
}
});
}
~Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>(this, "PopUpData");
}
我在发件人行上放了换行符,可以看到它只执行了一次,但在订阅者上,换行符显示了多次执行。我该怎么做才能避免这种情况?
您可以尝试传递密钥名称,而不是提供当前参考。
Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>("SomeKey", "PopUpData");
MessagingCenter.Subscribe<ChatCompartilhar, PassaParametro>("SomeKey", "PopUpData", async (sender, arg) =>
{
Task.WaitAll(tasks.ToArray());
switch (arg.Tipo)
{
case 2: tasks.Add(Task.Run(() => ShareMap(arg))); break;
case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
}
});
}
~Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>("SomeKey", "PopUpData");
}
我正在处理一个需要向另一个页面发送信息的页面,我正在通过消息中心执行此操作,但我注意到消息被发送一次并收到 2、4、6 次...取决于我打开订阅者页面的次数。奇怪的是我添加了一些代码以避免多次订阅。请参阅我的代码的相关部分:
发件人:
if (result.status == "ok")
{
MessagingCenter.Send<ChatCompartilhar, PassaParametro>(this, "PopUpData", new PassaParametro(){ Tipo = 3, Valor = result.msg, Extra = dataFinal });
await Navigation.PopPopupAsync();
}
订阅者:
Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>(this, "PopUpData");
MessagingCenter.Subscribe<ChatCompartilhar, PassaParametro>(this, "PopUpData", async (sender, arg) =>
{
Task.WaitAll(tasks.ToArray());
switch (arg.Tipo)
{
case 2: tasks.Add(Task.Run(() => ShareMap(arg))); break;
case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
}
});
}
~Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>(this, "PopUpData");
}
我在发件人行上放了换行符,可以看到它只执行了一次,但在订阅者上,换行符显示了多次执行。我该怎么做才能避免这种情况?
您可以尝试传递密钥名称,而不是提供当前参考。
Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>("SomeKey", "PopUpData");
MessagingCenter.Subscribe<ChatCompartilhar, PassaParametro>("SomeKey", "PopUpData", async (sender, arg) =>
{
Task.WaitAll(tasks.ToArray());
switch (arg.Tipo)
{
case 2: tasks.Add(Task.Run(() => ShareMap(arg))); break;
case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
}
});
}
~Page()
{
MessagingCenter.Unsubscribe<ChatCompartilhar, PassaParametro>("SomeKey", "PopUpData");
}