Xamarin forms MVVM Remove the previous VM in my current VM

Deos69Deos69 Member ✭✭

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.

Best Answer

Answers

  • Deos69Deos69 Member ✭✭

    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.

  • GraverobberGraverobber Member ✭✭✭

    Great glad it worked.

    Don't forget to unsubscribe the event in Registration OnDisappearing.

    protected override void OnAppearing()
    {
        Application.Current.ModalPopped += OnPop;
        base.OnAppearing();
    }
    
    protected override void OnDisappearing()
    {
        Application.Current.ModalPopped -= OnPop;
        base.OnDisappearing();
    }
    
    protected void OnPop(object sender, ModalPoppedEventArgs e)
    {
        await Navigation.PopModalAsync();
    }
    
Sign In or Register to comment.