0
votes

I am creating a xamarin forms app.My app flow is LOGIN-->MAINPAGE. The Mainpage is a Bottombarpage contains three options. 1.DashBoard, 2.Settings, 3.User . I added a logout icon on Dashboard class toolbar. My problem is whenever user click on logout, screen will navigate to login page. But if user press backbutton it will go to previous mainpage. I want to disable that.

Iam navigating from loginpage to Main page is like this.

 Navigation.InsertPageBefore(new MainPage(), this);
   await Navigation.PopAsync();

My App.xaml.cs - I using a validation for navigation to main page

if (Settings.Generalsettings != string.Empty)
            {

                MainPage = new NavigationPage(new MainPage());
            }
            else {
                MainPage = new NavigationPage(new Login());

            }

My logout button click on Dashboard.cs

   private  void logout_Clicked(object sender,EventArgs e)
        {

                    Settings.ClearAllData();           
                    Navigation.PushAsync(new Login());               

        }
2
set MainPage = new NavigationPage(new Login()); on logout_Clicked eventPrasanth
bro the logout button is created on the toolbar of Dashboard. Main page is bottombarAnand
it will redirect and add into new navigation stack order 'Login page' againPrasanth
Why you are using await Navigation.PopAsync(); while navigate from login page to main page. You should use like this Navigation.PushAsync(new MainPage());Prasanth
bro because i also want to block going back to login pager after loginAnand

2 Answers

1
votes

There are two ways one if you are using a view model then you can simply navigate like this

await _navigationService.NavigateAsync("app:///NavigationPage/Splash");

Second is if you are using forms only then you can set the main page with a login page

MainPage = new LoginPage();

This will clear your back stack and navigate to the particular page.

1
votes

Here you pushing a login page again, because the login page is already in navigation stack. So when you hit back button it show the login page.You can do like this MainPage = new NavigationPage(new MainPage()); From App.Xaml.cs page call

public App()
    {
        InitializeComponent(); MainPage = new LoginPage();

}

  // and when login button pressed from login page do like this
private  void loginButton_Clicked(object sender,EventArgs e)
    {
        NavigationPage navigationRootPage = new NavigationPage(new MainPage());
        navigationRootPage.BarTextColor = Color.White;
        MainPage = navigationRootPage;

    }

// when you hit logout button

private  void logout_Clicked(object sender,EventArgs e)
    {

                Settings.ClearAllData();           
                MainPage = new LoginPage();             

    }