In Xamarin.Forms v2.3.4.247, auf Beim Testen von Android haben wir festgestellt, dass die OnAppearing() -Methode der Navigationsseite aufgerufen wurde, wenn sich eine modale Seite über einer Navigationsseite befand und Sie die App verlassen, statt der modalen Seite, die sich tatsächlich oben befindet. Um zu arbeiten, haben wir Folgendes getan: - In der OnAppearing-Methode, überprüfen Sie, ob Sie tatsächlich oben sind - wenn Sie oben sind, alle gut - wenn Sie nicht an der Spitze sind, rufen Sie SendDisappearing() auf selbst und SendAppearing() auf der eigentlichen Seite oben. Wir haben außerdem festgestellt, dass Sie SendDisappearing() auf der obersten Seite aufrufen müssen, da Xamarin-Formulare diese Methoden vor mehreren Aufrufen schützt. Mit anderen Worten, wenn Sie SendDisappearing() zweimal aufrufen, wird es nur einmal ausgeführt.
Hier ist ein Beispiel, das für uns gearbeitet, da wir eine BaseContentPage aus denen haben alle Seiten ableiten:
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
}
}
wo ModalContentPageOnTop wie folgt aussieht:
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;
}
Wir haben eine sehr flache Navigationshierarchie hatte in unserer App funktioniert dieser Ansatz möglicherweise nicht mit komplizierteren Navigationsstapeln (dh modale Seiten, die andere Seiten oder andere modale Seiten verschieben)
Wenn die Aufrufe von OnAppearing und OnD erfolgen Isappearing 'sind nicht ausgeglichen, das ist ein Fehler. Ich empfehle, einen Bericht auf bugzilla.xamarin.com mit einem eigenständigen Beispiel einzureichen. – Krumelur
Workaround hier könnte immer abbestellen vor dem Abonnieren von Agian – Greensy