使用Mvvmcross登录页面

Login page using Mvvmcross
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我正在为Android,iPhone,WinRT和WinPhone编写一个需要用户首先登录的应用程序(它们可能会保存其凭据,因此在初次登录后可能需要省略此步骤).

我知道我可以将登录视图模型注册为起始页,但是当用户完成登录以防止导航回它时,我需要将其从后堆栈中删除.

因此,我决定转至FirstVM,如果现在有缓存的凭据,则转至Login VM.LoginVM只会调用close并使用一条消息通知FirstVM它将继续正常运行.

我正在作为WinRT应用程序进行初始实现,第二个选项的两个问题是:

  1. 从FirstMV导航到登录VM似乎没有什么好事.开始将是显而易见的选择,但是这将不允许导航,因此我必须执行以下操作:

    public async override void Start()
    {
        base.Start();
        await Task.Delay(1);
        ShowViewModel<LoginViewModel>();            
    }
    

    必须有更好的方法!

  2. 当我在LoginViewModel上调用close时,FirstVM收到了我发送的消息,但是WinRT创建了一个新的FirstVM并导航到该实例,而不是最初创建的实例.

无论如何,可能有更好的方法来完成所有这些工作!

谢谢

罗斯

解决过程1

WinRT与WinPhone / Silverlight具有不同的默认导航模型

http://mikaelkoskinen.net/winrt-navigation-cache-or-why-going-back-to-previous-page-creates-a-new-instance-of-the-page/上有很好的解释-包括如何模仿WinPhone

I'm writing an application for Android, iPhone, WinRT and WinPhone that requires the user to login first (they may save their credentials so this step may need to be omitted after the initial login).

I know I can register the login view model as the start page, but then I would need to remove it from the back stack when the user completes the login to prevent navigation back to it.

So instead I decided I would navigate to the FirstVM, and if there are now cached credentials I would navigate to the Login VM. The LoginVM would just call close and use a message to inform the FirstVM it may continue as normal.

I'm doing the initial implementation as a WinRT app, and the two issues I have with the second option are:-

  1. There appears to be no good event to use to navigate to the login VM from the FirstMV. Start would be the obvious choice, but this won't allow a navigation so I have had to do the following:

    public async override void Start()
    {
        base.Start();
        await Task.Delay(1);
        ShowViewModel<LoginViewModel>();            
    }
    

    There must be a better method!

  2. When I call close on the LoginViewModel the message I sent is recieved by the FirstVM, but WinRT creates a new FirstVM and navigates to that instance, instead of the one initially created.

There is probably a much better way to do all of this anyway!

Thanks

Ross

Solutions1

WinRT has a different default navigation model to WinPhone/Silverlight

There's a good explanation of it on http://mikaelkoskinen.net/winrt-navigation-cache-or-why-going-back-to-previous-page-creates-a-new-instance-of-the-page/ - including how to mimic WinPhone

转载于:https://stackoverflow.com/questions/23733690/login-page-using-mvvmcross

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入