How to use ViewModelCloser to close the View of the ViewModel?

天大地大妈咪最大 提交于 2019-12-11 02:14:22

问题


In the MvvmCross v3, CustomerManagement example, the method void RequestClose(IMvxViewModel viewModel) closes the top View. How do you close the View of a ViewModel instead?


回答1:


I wouldn't use that ViewModelCloser method - although it could be extended if you want to.

MvvmCross v3 removed the previous CloseViewModel method - because it didn't really work across all platforms and across all presentation styles - across all of navigation controllers, splitviews, tabs, flyouts, popups, dialogs, etc.

To replace it, v3 introduces a new ViewModel call:

    protected bool ChangePresentation(MvxPresentationHint hint)

This is matched in the UIs with an IMvxViewPresenter method:

    void ChangePresentation(MvxPresentationHint hint);

To use this, you will need to:

  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.

I realise this is 'more work' than was required in vNext, but hopefully it's a more flexible, powerful approach.



来源:https://stackoverflow.com/questions/15919288/how-to-use-viewmodelcloser-to-close-the-view-of-the-viewmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!