Multiple messages from Xamarin Message Center when restarting the application

I have an application Xamarin Formsin which I use MessagingCenterto send some data from a specific platform to the application Xamarin.Forms.

In mine, PageI subscribed to posts in base.OnAppearing()and unsubscribed in the method base.OnDisappearing(). This works as expected.

The problem I am facing is that when the application stops at AndroidOS(an example of this is when I change the device language), I start to receive copies of messages. I am confused why this happens, but I noticed that the method is base.OnDisappearing()not called when the application restarts.

Does anyone know what might cause my problem and how to fix it? Also, is there a way to Xamarinsee all your publishers and subscribers?

+4
source share
4 answers

As Grincy said, in addition to subscribing to OnAppearing()and unsubscribing in OnDisappearing(), I also unsubscribed from the message before subscribing, because, why not:

protected override async void OnAppearing() {
    base.OnAppearing();

    MessagingCenter.Unsubscribe<string>(this, "KeyHere");
    MessagingCenter.Subscribe<string>(this, "KeyHere", string => { }));
}
+3
source

Xamarin.Forms v2.3.4.247 Android , , , , OnAppearing() , . , : - OnAppearing , - , - , SendDisappearing() SendAppearing() . , SendDisappearing() , Xamarin . , SendDisappearing(), .

, , BaseContentPage, :

protected override void OnAppearing()
{
    base.OnAppearing();
    var pageOnTop = ModalContentPageOnTop();
    if (pageOnTop == null || pageOnTop == this)
    {
        // do some stuff, like setting up subscriptions
        SetupSubscriptions();
    }
    else
    {
        // Xamarin Forms Appearing/Disappearing Bug
        // Found that when you leave the Android app with a modal page on top of a non-modal page (either minimise the app or lock the device), then Xamarin.Forms does not properly call OnAppearing. 
        // Xamarin.Forms will call OnAppearing on the non-modal page and not on the modal page. This would not only cause message subscriptions to not be set up on the modal page, 
        // but would also set up message subscriptions on the non-modal page. Not good.  Hopefully, this work-around should stop being executed when Xamarin fixes the problem, as we won't 
        // fall into this "else" condition above, because OnAppearing will be called on the right page.

        // NOTE: SendDisappearing and SendAppearing have boolean guards on them. If SendAppearing was called last, calling it again will do nothing.
        // SendDisappearing has the same behaviour. Thus we need to call SendDisappearing first, to be guaranteed that SendAppearing will cause the OnAppearing() method to execute.
        ((IPageController)pageOnTop).SendDisappearing();  // try and correct Xamarin.Forms - the page actually on top was not told it was disappearing - tell it now
        ((IPageController)this).SendDisappearing();  // try and correct Xamarin.Forms - if it thinks the view has appeared when it hasn't, it won't call OnAppearing when it is truly appearing.
        ((IPageController)pageOnTop).SendAppearing();  // try and correct Xamarin.Forms by notifying the correct page that it is appearing
    }
}

ModalContentPageOnTop :

private ContentPage ModalContentPageOnTop()
{
    var page = Navigation.ModalStack.LastOrDefault();
    var navigationPage = page as NavigationPage;
    if (navigationPage != null)
        return navigationPage.CurrentPage as ContentPage;
    return page as ContentPage;
}

, ( , )

+1

, :

MessagingCenter.Subscribe<string, DetailClass>(this, "KeyHere", async (detail) =>  
{
   // do someting....
   MessagingCenter.Unsubscribe<string,DetailClass>(this, "KeyHere");
});

.

0

OnAppearing OnDisappearing InitializeComponent() :

protected override void OnParentSet()
{
    base.OnParentSet();

    if (Parent == null)
    {
        // unsubscribe 
    }
}

OnParentSetcalled before OnAppearingand after OnDisappearing. However, the parent only becomes null on the page after it is removed from the navigation stack or when the page appears. This ensures that both of them are called only once, while I noticed that OnAppearingthey are OnDisappearingnot always called sequentially depending on how pop-ups and / or other things can be displayed.

0
source

Source: https://habr.com/ru/post/1655479/


All Articles