如何使用ViewModelCloser关闭ViewModel的View?

3
在MvvmCross v3的CustomerManagement示例中,方法void RequestClose(IMvxViewModel viewModel)会关闭顶部View。那么如何关闭ViewModelView呢?
1个回答

5

我不建议使用那个ViewModelCloser方法 - 尽管如果您想要可以扩展它。

MvvmCross v3已经移除了之前的CloseViewModel方法 - 因为它不能在所有平台和所有呈现风格中正常工作 - 无论是在导航控制器、分割视图、选项卡、弹出窗口、对话框等中。

为了替代它,v3引入了一个新的ViewModel调用:

    protected bool ChangePresentation(MvxPresentationHint hint)

在UI中,这与IMvxViewPresenter方法匹配:

    void ChangePresentation(MvxPresentationHint hint);

要使用此功能,您需要:

  1. Create a new Hint class - e.g. public class CustomPresentationHint : MvxPresentationHint { /* ... */ }

  2. In each UI project, provide a custom presenter (normally by overriding CreateViewPresenter() in your Setup.cs class) - and in that custom presenter handle the ChangePresentationHint call:

          public void ChangePresentation(MvxPresentationHint hint)
          {
              if (hint is CustomPresentationHint)
              {
                   // your custom actions here
                   // - which may involve interacting with the RootFrame, with a NavigationController, with the AndroidFragment manager, etc
              }
          }
    
  3. In your viewmodel, you can send a CustomPresentationHint when you want to.

我知道这比vNext要多做一些工作,但希望这是一种更加灵活、强大的方法。

我已经看过ChangePresentation(MvxPresentationHint hint)方法了,我猜测这个hint可以存储任何信息的MvxBundle。然而,问题是我在ViewModel中调用ChangePresentation,我不知道如何将与ViewModel相关的View传递给Presenter。 - Steven Chan
这是你的代码 - 你可以随心所欲地进行修改 - 比如,你可以在你的 CusomPresentationHint 中添加一个 public IMvxViewModel ViewModelToClose {get;set;} 属性。 - Stuart
CustomPresentationHint hint = new CustomPresentationHint(); hint.ViewModelToClose = this; ChangePresentation(hint);Presenter 只获取 ViewModel,如何找到对应的 View? - Steven Chan
这取决于视图的呈现方式。演示者拥有视图 - 每个视图都通过演示者中的ShowView显示。 - Stuart
我认为我只需在Presenter中保留一个视图-视图模型映射。 - Steven Chan
请参阅以下链接中的更新内容:https://dev59.com/eWQo5IYBdhLWcg3wUN7Z - Stuart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接