Navigate to a new mvvm page - without keeping last page on stack (eg back button can't show it)

PaulH.4499PaulH.4499 USMember
edited June 2015 in Xamarin.Forms

My login view model works well and it navigates nicely to the main view model of the app. But how do I close/finish the login page when moving forward so the user can't go back to it and it is not on the stack any more?

LoginViewModel:
public ICommand SubmitCommand
{
....
_navigationService.NavigateTo(Locator.HomePage);
}

I currently use MvvmLite.
Thanks.

Tagged:

Posts

  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    Hi,

    I believe the approach to use is to replace the MainPage of your App class after the login has occurred. This approach is detailed in the following code :-

    https://github.com/conceptdev/xamarin-forms-samples/blob/master/LoginDemo/LoginPattern/App.cs

    Hope this helps.

    Paul Diston

  • PaulH.4499PaulH.4499 USMember

    It looks like my login page is a navigation page and the NavigateTo calls _navigation.PushAsync(page);

    Therefore making login page stay on the stack - perhaps I have to start the stack not in App.cs but later, OR I should just pop the stack just before I push the next page (but that did not seem to work).

  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    Hi,

    If you replace the MainPage of your App class then this will clear the stack, so you can set the MainPage to a new instance of a NavigationPage, setting the root to a new ContentPage, for example.

    Thanks

    Paul Diston

  • ElvinAsadovElvinAsadov AZMember ✭✭

    @PaulDiston, It is bug for windows Phone if you will try to change MainPage more than one time. Because changing Main Page >1 times Alert will not wait to user respond on windows phone. For this reason i can not deploy my WindowsPhone version(
    Please see https://bugzilla.xamarin.com/show_bug.cgi?id=25473

  • ylemsoulylemsoul RUMember ✭✭✭
    edited June 2015

    Not sure if it is a good practice (tested in WP and Android):
    public partial class Page1 { // .. private async void Button_OnClicked(object sender, EventArgs e) { await this.Navigation.PushAsync(new Page2()); this.Navigation.RemovePage(this); } }

    Don't know how to do this with MVVMLight's INavigationService Perhaps, you need to wrap it with custom service or better refactor like @PaulDiston suggested.

  • PaulH.4499PaulH.4499 USMember

    Thanks for the logical answers. I will try the MainPage way as we currently are supporting Android and iOS. I didn't work on my first quick attempt - maybe because I am trying to initialise the navigation service inside the login view model. I will try again later on, and will let you know if it solves it.
    I tried the removepage idea with MvvmLight before posting this questions but it didn't expose the Navigation object nor provide that method - only pop, but if I pop the root element... might not work well!
    I will let you know when I have something working! CHeers

  • SivaShankarArumugamSivaShankarArumugam USMember ✭✭✭

    Facing the same issue

Sign In or Register to comment.