简体   繁体   English

Xamarin形成+ prism登录流程

[英]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. 我是Xamarin Forms的新手(也是Prism中的新手)我正在尝试创建我的第一个xamarin表单应用程序并需要一个登录流程。

I tried to check if user logged in application.OnInitialized method (and show Login or Main page depending on the result). 我试图检查用户是否登录了application.OnInitialized方法(并根据结果显示Login或Main页面)。 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. 然后我试图在主页面的OnNavigationTo中检查这个,但它也不起作用。 I checked this in PRE version and stable version and had different errors. 我在PRE版本和稳定版本中检查了这个并且有不同的错误。 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. 然后我尝试将导航服务注入MainPage(不是视图模型),但我发现导航服务无法注入那里。

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. 然后我决定在base之后从view发送消息.OnAppearing并在视图模型中订阅该消息,并根据需要进行导航登录作为回调。 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. 如果我在MainPage上使用ToolBarItems,那么在Windows手机上它从登录页面导航后就消失了(但突然,Windows手机上的登录页面有工具栏项目),看起来像Windows手机上的OnAppearing方法在工具栏项目加载之前触发。 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. 然后我决定添加一个空白页面并在base.onAppearing进入视图模型后发送消息,在该视图模型中我重定向到Login或主页面。 So, it works with one small "BUT". 所以,它适用于一个小“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. 请记住,OnNavigatedTo仅在使用最新预览版本并调用NavigationService.Navigate时才有效。 There are a couple of approaches to take here. 这里有几种方法。 First you can navigate to MainPage, then check in the OnNavigatedTo. 首先,您可以导航到MainPage,然后检查OnNavigatedTo。 If not logged in, navigate to the LoginPage. 如果未登录,请导航到LoginPage。 You could also just check on App startup. 你也可以检查App启动。 When you show the LoginPage, you can use an absolute URI to replace the entire navigation stack. 显示LoginPage时,可以使用绝对URI替换整个导航堆栈。 Essentially what this does is set MainPage = new MainPage(). 基本上这是设置MainPage = new MainPage()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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