Xamarin Forms Navigation and working with the login page

I am trying to create an application with a login page as the first page.

As soon as the user logs in, the pages that appear after that will be in the standard organization of the page stack, so I can easily use the design in Navigation and wrap everything on the navigation pages.

eg.

 Login Page -> MainAppPage |-> Category1Page -> Cat1SubPage |-> Category2Page -> Cat2SubPage 

I understand that I have to wrap MainAppPage with new NavigationPage() , and then I will have access to the Navigation object, allowing me to do such things:

 await this.Navigation.PushAsync(new Category1Page()); 

And the various platforms will give me automatic back button support to return to the previous page.

But I don’t want the user to move from LoginPage -> MainAppPage this way, because I don’t want the backbutton to return them back to Login, without having to explicitly click the logout button.

So, how should I handle this first page transition from the LoginPage -> MainApp site.

Is there an alternative way to have 2 Primary pages and change them among themselves? Or is there a way to intercept back button requests on MainAppPage and drop them?

Not having found most of the information in the documentation, but this seems like a fairly standard requirement, so PEBKAC is possible

+6
source share
5 answers

I can think of at least two solutions.

One way is to create MainAppPage first, and on this page show the login page as modal.

Another would be to create a page with a specific platform, load the login page, and only after successfully logging into MainPage using platform navigation (LoginPage must be NoHistory or something like that to avoid returning), and not in the navigation form (for example, in Android, pages should be separate activities). This requires a bit more work, since you have to deal with three platforms, but it should not be too overhead.

This suggests a better navigation, presumably with hope, 1.3.0.

+3
source

I just posted a quick Github example for this scenario. The idea is that you want to go to your NavigationPage first, and then, if necessary (which means that the user is not logged in yet), click LoginPage by default. Then, upon successful login, just pull LoginPage from the stack. Here you can check the sample, https://github.com/jamesqquick/Xamarin-Forms-Login-Navigation/blob/master/ReadMe.MD

Screenshot from HomePage

+5
source

Here is what I am working on Android:

 protected override void OnCreate (Bundle bundle){ base.OnCreate (bundle); string start = "new"; Bundle extras = Intent.Extras; if (extras != null) { start = extras.GetString ("start"); } if(start == "new"){ SetPage (App.GetLoginPage (OnLoginCompleted)); } else if (start == "login") { SetPage (App.GetMainPage (OnSignOutCompleted)); } } void OnLoginCompleted(){ // ... var refresh = new Intent (this, typeof(MainActivity)); refresh.PutExtra ("start", "login"); StartActivity (refresh); Finish (); } void OnSignOutCompleted(){/* mirrors OnLoginCompleted */ } 

This is an effective activity with a custom landing page. To change it, we restart it with a different setting. This is a bit slower than on my phone, but only noticeable.

0
source

As Micah Markik said, a modal window is a good option. One more thing you can also consider, especially if you want the login page to have the same navigation bar as your other pages, would be the same as I already posted at the following URL.

Basically, you save the link to your NavigationPage in the App class (call it AppNavPage ), and then, showing your login page, you put your login page in a separate NavigationPage and do PushAsync() using the new NavigationPage and login page.

Once the user logs in successfully, you simply replace the current MainPage with the old NavigationPage using this code: Application.Current.MainPage = App.AppNavPage

Check out the link below for better code examples.

fooobar.com/questions/977077 / ...

0
source

I think the best way would be to remove LoginPage from the stack after confirming the login, and then it is no longer available.

 async void OnLoginButtonClicked (object sender, EventArgs e) { ... var isValid = AreCredentialsCorrect (user); if (isValid) { App.IsUserLoggedIn = true; Navigation.InsertPageBefore (new MainPage (), this); await Navigation.PopAsync (); } else { // Login failed } } 

Provided that the user credentials are correct, the MainPage instance is inserted into the navigation stack up to the current page. Then the PopAsync method removes the current page from the navigation stack, and the MainPage instance becomes the active page.

See full description here

0
source

Source: https://habr.com/ru/post/977075/


All Articles