简体   繁体   中英

Xamarin forms + prism login flow

I'm new in Xamarin Forms (and in Prism as well) I'm trying to create my first xamarin forms application and need a login flow.

I tried to check if user logged in application.OnInitialized method (and show Login or Main page depending on the result). But the problem is that if i show Login page and then after login i show Main page, then user is able to navigate to login page using hardware buttons..

Then i tried to check this in OnNavigationTo in Main page, but it's not working also. I checked this in PRE version and stable version and had different errors. In one of them the navigation just didn't work, in other - i got an error, that main page couldn't be created.

Then i tried to inject navigation service into MainPage (not a view model), but i found out that navigation service couldn't be injected there.

Then i decided to send a message from view after base.OnAppearing and subscribe to that message in view model and do navigation to login if needed as a callback. But there's one very strange problem. If i'm using ToolBarItems on a MainPage, then on windows phone it's disappearing after navigation back from login page (but suddenly, login page on windows phone HAS that toolbar items), Looks like OnAppearing method on windows phone fires before toolbar items are loaded. In any case, it's not a solution.

Then i decided to add a blank page and send a message after base.onAppearing into a view model and inside that view model i do redirect to Login or main page. So, it works with one small "BUT". If i press hardware back button on login page or main page i don't close application as i navigate to blank page which redirects me to login or main page.

Is there a proper way to implement login flow using prism? i really like it as it's very powerful framework.. Thanks in advance!

Keep in mind that OnNavigatedTo only works when using the latest preview version, and calling NavigationService.Navigate. There are a couple of approaches to take here. First you can navigate to MainPage, then check in the OnNavigatedTo. If not logged in, navigate to the LoginPage. You could also just check on App startup. When you show the LoginPage, you can use an absolute URI to replace the entire navigation stack. Essentially what this does is set MainPage = new MainPage().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM