Xamarin MessagingCenter:发送一次,接收多个]]

问题描述 投票:2回答:1

我在xamarin邮件中心遇到麻烦的问题。我添加了一些断点,并注意到仅发送了一条消息,但订阅者收到了两次。

我的发件人代码(Page2):

async void CompartilhaMapa(System.Object sender, System.EventArgs e)
{ 

    ...        

   MessagingCenter.Send<Page2, ParamType>(this, "PopUpData", new ParamType() { Tipo = 2, Valor = Coords });
   Console.WriteLine("msg sent"); //displayed only once and line above has a break line assuring this
   await Navigation.PopPopupAsync();
}

第1页(订阅者):

protected override void OnAppearing()
{
   base.OnAppearing();
   MessagingCenter.Subscribe<Page2, ParamType>(this, "PopUpData", async (sender, arg) =>
   {
      Task.WaitAll(tasks.ToArray());
      switch (arg.Tipo)
      {
          case 2:
             Console.WriteLine(" *********** msg received"); //this is outputted twice
             tasks.Add(Task.Run(() => ShareMap(arg))); //this is called twice
             break;
             case 3: tasks.Add(Task.Run(() => ShareEvent(arg))); break;
             case 6: tasks.Add(Task.Run(() => ShareImage(arg))); break;
      }
    });
}

protected override void OnDisappearing()
{
   base.OnDisappearing();
   MessagingCenter.Unsubscribe<Page2, ParamType>(this, "PopUpData");
}

我在这里做错了什么?

我在xamarin邮件中心遇到麻烦的问题。我添加了一些断点,并注意到仅发送了一条消息,但订阅者收到了两次。我的发件人代码(Page2):...

c# xamarin messaging messagingcenter
1个回答
0
投票

有两个可能的原因:

© www.soinside.com 2019 - 2024. All rights reserved.