I have a main page : HomeVM. When the app starts, I use PushModalAsync for RegistrationVM In my RegistrationVM, I have a button for pushModalAsync -> LoginVM
When the user is connected, I want go back in the VM HomeVM, but impossible, with PopModalAsync, I come back to RegistrationVM.
I would like to close all popmodalasync.
I have tried this
Here in my app.xaml :
var mainPage = (Page)ViewFactory.CreatePage(typeof(HomeVM)); MainPage = new NavigationPage(mainPage) { BarBackgroundColor = (Color)Resources["PrimaryColor"], BarTextColor = Color.White, }; Navigation = MainPage.Navigation; protected override void OnStart() { var registrationpage = (Page)ViewFactory.CreatePage(typeof(RegistrationVM)); MainPage.Navigation.PushModalAsync(registrationpage); }
RegistrationVM :
public RegistrationVM() { Task.Run(async () => await ConnectionAPI()); } async Task ConnectionAPI() { try { applicationContext.Device = mydevice; await Navigation.PushModalAsync<LoginVM>(async (vm, p) => await vm.InitializeAsync(this)); } catch (Exception e) { Log.Error(e, "Unhandled exception while loading touch points : {e}", e); } finally { } }
LoginVM : When the user click on "Login" PreviousVM = RegistrationVM.
async Task Login() { try { Log.Information("Logging in"); applicationContext.User = employe; await Navigation.PopModalAsync(); await Navigation.RemoveAsync(previousVM); } catch (Exception e) { Log.Error(e, "Unhandled exception during log in : {e}", e); } finally { } }
I come back to Registration view
I have tried by using "PushAsync" and "PopAsync" and try this in LoginVM.cs : await Navigation.PopToRootAsync();
Nothing works correctly. It always comeback to RegistrationVM.
I have the theory that you Main.Navigation, the one where you push your Regpage to is not the same as the navigation of Regpage.
Therefore when you tried popToRoot in your login, it just popped back to Regpage.
Edit: To test this theory you could put a breakpoint into your LoginVM and check the Navigation params stack there.
Here are some thoughts to your code
As you're working with modal pages, one thing you could try:
In your RegistrationVM register for the Apps ModalPopped event. When it fires pop the registration page as well
Application.Current.ModalPopped += (sender, e) =>
{
await Navigation.PopModalAsync();
};
If you try this, remove this line from your LoginVM await Navigation.RemoveAsync(previousVM);
Answers
I have the theory that you Main.Navigation, the one where you push your Regpage to is not the same as the navigation of Regpage.
Therefore when you tried popToRoot in your login, it just popped back to Regpage.
Edit: To test this theory you could put a breakpoint into your LoginVM and check the Navigation params stack there.
Here are some thoughts to your code
As you're working with modal pages, one thing you could try:
In your RegistrationVM register for the Apps ModalPopped event. When it fires pop the registration page as well
Application.Current.ModalPopped += (sender, e) =>
{
await Navigation.PopModalAsync();
};
If you try this, remove this line from your LoginVM await Navigation.RemoveAsync(previousVM);
Thanks for your answer!
First, I can't write MainPage = new NavigationPage(new HomeVM());
HomeVM is is not recognized
I have tried :
Application.Current.ModalPopped += (sender, e) =>
{
await Navigation.PopModalAsync();
};
It works for me cause Registration is the first Page I think, so with this code, i'm waiting LoginVM closure and RegistrationVM close at his turn.
Great glad it worked.
Don't forget to unsubscribe the event in Registration OnDisappearing.