4

I am creating an application and in that it need to send argument to the other page and send argument using the Messagingcenter when I send using MessagingCenter it call more than one time.

If I am using unscribe than in next time it is not receive next time.

MessagingCenter.Send(this, "Invoke", "Invokedtrue");

MessagingCenter.Subscribe<MyPage, string>(this, "Invoke", async (sender, arg) =>
{
    await Process(arg);
});
MessagingCenter.Unsubscribe<MyPage>(this,"Invoke");


**ListPage**

    private void ViewCell_Tapped(object sender, EventArgs e)
    {
        try
        {
            MessagingCenter.Send(this, "Invoke", "Invokedtrue");
        }
        catch (Exception ex)
        {
            Debug.Write(ex);
        }
    }

**Detail Page**

    MessagingCenter.Subscribe<ListPage, string>(this, "Invoke", async (sender, arg) =>
    {
        await Process(arg);
    });

    private async Task Process(string arg)
    {
        //Here is api call to view detail of particular record

        //Here I unsubscribe the MessagingCenter.
        MessagingCenter.Unsubscribe<ListPage>(this,"Invoke");
    }

I want to send only one time using subscribe and send only one time.

Can anyone look into this and suggest me what should I have to do in that?

Deep Soni
  • 431
  • 4
  • 24
  • Are you saying the subscribe hits multiple times in the second page? – Bruno Caceiro Jun 13 '19 at 14:26
  • 1
    are you sure you are not calling send or subscribe multiple times? Also, the arguments you use in Send should match those you use when calling Subscribe – Jason Jun 13 '19 at 14:26
  • Hi @Jason Yes I am sure that I only click single time but it subscribe more than one time. So execution process takes too long. Can you please suggest what should we have to do in that? – Deep Soni Jun 14 '19 at 04:07

1 Answers1

3

Do you want to send and receive message only one time? Then you can use Unsubscribe method after you receive the message. For example, you can do like this:

 MessagingCenter.Subscribe<MyPage, string>(this, "Invoke", async (sender, 
   arg) =>
{
await Process(arg); 
MessagingCenter.Unsubscribe<MyPage>(this,"Invoke");
});

Updated:

On DetailPage, you can call MessagingCenter.Subscribe in method OnAppearing() and call MessagingCenter.Unsubscribe in method OnDisappearing , just as follows:

protected override void OnAppearing()
    {
        base.OnAppearing();
       MessagingCenter.Subscribe<ListPage, string>(this, "Invoke", async (sender, arg) =>
        {
            Debug.Write("123456---->  get one msg");
            DisplayAlert("Alert", "We have received a message.", "OK");
        });
    }

    protected async override void OnDisappearing()
    {
        base.OnDisappearing();
        MessagingCenter.Unsubscribe<ListPage,string>(this, "Invoke");
    }
}

On ListPage

async void OnTap (object sender, EventArgs e)  
    {
        await Navigation.PushAsync(new DetailPage());
        try
        {

            MessagingCenter.Send(this, "Invoke", "Invokedtrue");

            Debug.Write("123456---->  send one msg");

        }
        catch (Exception ex)
        {
            Debug.Write(ex);
        }

    }

Note: when you enter into ListPage, you can try the following code:

  MainPage = new  NavigationPage( new ListPage());

The effect in IOS is : enter image description here

The link of full demo is here, you can check it.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • I have try with this but when user click on second time than also not getting subscribe in my page. Can you please give other best solution? – Deep Soni Jun 14 '19 at 04:08
  • @DeepSoni Do you want to get the message again after that ? The mechanism of MessagingCenter is to send once and receive once, unless you unsubscribe it. Could you please share more code snippet about MessagingCenter in your app? – Jessie Zhang -MSFT Jun 14 '19 at 06:01
  • I add my code in a question can you please look into that and suggest me what should I have to change? – Deep Soni Jun 14 '19 at 08:42
  • @DeepSoni I have updated the answer, you can check it. – Jessie Zhang -MSFT Jun 14 '19 at 10:15
  • Thanks for response. I have try with your code and works fine on `android` but when I move platform to `iOS` than it doesn't work for me. Can you please check and share possible way that work good on both platform. – Deep Soni Jun 20 '19 at 05:30
  • @DeepSoni The MessagingCenter is fine. What we need to pay attention to is the push and pop styles of the page. I have updated the answer , you can check it. – Jessie Zhang -MSFT Jun 20 '19 at 07:39
  • Thanks for response. Let I try with your updated code in both platform. – Deep Soni Jun 24 '19 at 13:58
  • I have tried what you have mentioned still the subscribe get called twice. – Divyesh Apr 16 '21 at 10:02