简体   繁体   English

Mvvmcross:处理全局应用程序事件

[英]Mvvmcross: handling global app events

Using MvvmCross, what's the beast way to implement global Application events? 使用MvvmCross,实现全局Application事件的野兽方法是什么?

Case 1: app launch - i want to PIN protect my app, so on FinishedLaunching i want some object to check if the PIN has been set and, in case it has not, launch a modal view to ask the user for PIN. 情况1:启动应用程序-我想用PIN码保护我的应用程序,因此在FinishedLaunching上,我希望某个对象检查是否已设置PIN码,如果尚未设置,请启动一个模式视图以询问用户PIN码。 The modal view should hide the underneath navigation without popping or modifying any loaded view. 模态视图应隐藏下面的导航,而不会弹出或修改任何已加载的视图。

Case 2: logoff - the user, through an mvvm command, removes the PIN from the app and the modal view blocks all other views without unloading them. 情况2:注销-用户通过mvvm命令从应用程序中删除了PIN,并且模式视图阻止了所有其他视图而不卸载它们。 In this case the model that stores the PIN publishes an event and some should get that event showing the modal view. 在这种情况下,存储PIN的模型将发布一个事件,并且应该会显示该事件以显示模式视图。

This could be easy to implement on the appdelegate, but i would like to know if there is a better MVVM way of doing this, some kind of ViewModel for the app itself that responds to events exposing the behaviour in the core library. 这可能很容易在appdelegate上实现,但是我想知道是否有更好的MVVM方式,一种针对应用程序本身的ViewModel,它可以响应暴露核心库中行为的事件。

If you want these type of events to be handled at an app/singleton level, then you can easily do this using: 如果您希望在应用程序/单个级别上处理这些类型的事件,则可以使用以下命令轻松地执行此操作:

  1. some kind of specialist service initialised in each UI project which uses the Messenger plugin to listen to messages 在每个UI项目中初始化的某种专业服务,该服务使用Messenger插件收听消息
  2. some kind of native object on each platform to send messages when key native events happen 每个平台上都有某种本机对象,可在关键本机事件发生时发送消息
  3. additional ICommand implementations in ViewModel s to send User-initiated events ViewModel的其他ICommand实现以发送用户启动的事件

MvvmCross does actually contain an initial implementation to help with 2 - each platform provides a singleton IMvxLifetime object MvvmCross实际上确实包含一个初始实现以帮助2-每个平台都提供一个单例IMvxLifetime对象

This is implemented on each platform using: 在每个平台上使用以下方法实现:

However, in practice this IMvxLifetime implementation has never been particularly heavily used - certainly when I've used it a couple of times I've found myself having to workaround issues on a few platforms. 但是,实际上,这种IMvxLifetime实施从未被特别大量地使用-肯定是当我使用几次后,我发现自己不得不解决一些平台上的问题。

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

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