Xamarin forms navigation backpress prevent on logout - xamarin.forms

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());
}

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.

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();
}

Related

removing root page from navigation stack?

my pages are
Page 2 (root page and using it as a splash screen progress bar) -> Page1
now what i want is when Page 2 finishes doing some logic it goes to page 1 and make Page1 my root page so that when user click back button it exits the application
i dont want when user press back button to navigate to Page 2 (root page and using it as a splash screen progress bar)
how can i implent that and here is my Code
App.xaml.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new Page2());
}
Page2.cs
public partial class Page2 : ContentPage
{
public Page2()
{
InitializeComponent();
.....some logic code ....
Navigation.PushAsync(new NavigationPage(new Page1()));
}
and in my page1.cs
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
App.Current.MainPage = new NavigationPage(new Page1());
Navigation.RemovePage(new Page2());
}
it appears black Screen , IDK how to solve this issue , thx :)
Page 2 finishes doing some logic it goes to page 1 and make Page1 my
root page
in Page2, whenever your code "finishes"
App.Current.MainPage = new NavigationPage(new Page1());

Xamarin forms: SwipeGestureRecognizer also enable MasterDetailPage Navigation Drawer?

I have implemented the swiping feature using SwipeGestureRecognizer on my homepage. But the problem is the home page is a child of a MasterDetailPage. So when I swipe to left in the homepage, the navigation drawer is also coming to UI. I have tried like below to stop showing navigation drawer on UI when doing SwipeGestureRecognizer using MessagingCenter, but no luck!!!
On Homepage
public void Swipe(object sender, EventArgs e)
{
MessagingCenter.Send<HomePage>(this, "HomePageSwipe");
//code for swipe
}
On MasterDetailPage Constructor
MessagingCenter.Subscribe<HomePage1>(this, "HomePageSwipe", (sender) =>
{
IsPresented = false;
NavigationPage.SetHasNavigationBar(this, false);
});
Is there any way to fix this?
I believe this behavior occurs only on ios. To disable this, you need to add this to the OnAppearing() method
if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.iOS)
{
IsGestureEnabled = false;
}

Get toolbar in Xamarin.Forms if the first page is not a navigation page

I tried to intercept the software back button in Android. I follow several samples that adds these:`
protected override void OnPostCreate(Bundle savedInstanceState)
{
var toolBar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolBar);
base.OnPostCreate(savedInstanceState);
}
protected override void OnResume()
{
Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
SetSupportActionBar(toolbar);
base.OnResume();
}
Then use the OnOptionItemSelected to get the software back button event.
But this didn't work because, for me the first page of the app is login page that does not belong to NavigationPage. After the successful login, I change the Application.Current.MainPage to the root application page that is a NavigationPage. But it is too late, the OnPostCreate or OnResume already been called and the FindViewById(Resource.Id.toolbar) return null.
Any suggestion?
I dont understand whether you are asking xamarin.forms or xamarin.android.
For xamarin.forms
You can navigate from Login Page to Mainpage like this
Navigation.InsertPageBefore(new MainPage(), this);
Navigation.PopAsync();
And In your App.xaml.cs
MainPage = new NavigationPage(new Login());

Xamarin forms navigate to modal page from navigation page

I'm having trouble with navigating to a modal page.
My MainPage in App() is set to a NavigationPage like so:
MainPage = new NavigationPage(new StartPage());
I'm trying to push a new page onto the stack from the StartPage like this:
public async void DisplayAdd()
{
await Navigation.PushModalAsync(new AddGameModalPage());
}
But I'm getting an error saying that it's not compatible with the Xamarin.Forms.Page.
I've also tried something like this, but the error still remains.
var page = new NavigationPage(new AddGameModalPage());
Navigation.PushModalAsync(page);

From App.cs I am going to my landing page(MainPage.xaml) and then want to navigate to other page?

I am navigation to MainPage.xaml from my App.cs following is my code for App.cs
public class App : BaseApplication
{
public App()
{
MainPage = new NavigationPage(new MainPage());
}
}
And in my MainPage.xaml and code behind I am doing following
public partial class MainPage:ContentPage
{
public MainPage()
{
InitializeComponent();
SetPage();
}
async public void SetPage()
{
//await Navigation.PushModalAsync(new Login());
await Navigation.PushAsync(new Login());
Navigation.RemovePage(this);
}
}
This code gives me the login page but doesn't remove MainPage from stack and keeps my Android back button enable and clicking on that icon navigates me back. I really want to set Login page as a starting page in some situation. I have also tried App.Current.MainPage = new NavigationPage(new Login()); but that didn't work also?
Any suggestion how do I set the Starting page dynamically from my mainpage.cs, note I don't want to do that in App.cs as I want to keep that as clean as possible?
For more details, on error, please click on this link
I currently have a solution with the same approach you want to do.
What I do there is something like this:
public async void LoginClick(object sender, EventArgs e){
try
{
//Some logic code here ...
App.Current.MainPage = new NavigationPage(new HomeView());
}
catch (Exception exc)
{
DisplayAlert("Error",exc.Message,"OK");
}
}
}

Resources