How to use ViewModelCloser to close the View of th

2019-07-20 06:56发布

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条回答
神经病院院长
2楼-- · 2019-07-20 07:20

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.

查看更多
登录 后发表回答