Handling MvxViewModel::Close when using custom vie

2019-08-27 21:11发布

I am using a custom view presenter in my Windows Store app.

I show a SettingsFlyout and in its SettingsViewModel view-model I have a command bound to a button on the flyout which needs to hide the flyout (SettingsFlyout::Hide()).

For this, I have different options:

  • in the SettingsFlyout, I can handle the button click

  • in the ICommand in the view-model, send a message from view-model to view

  • in the ICommand in the view-model, call Close(this).

    I was trying to implement last one, using Close(this) in the view-model.

    For this, in the custom view presenter I was thinking to handle ChangePresentation(MvxPresentationHint hint) or Close(IMvxViewModel viewModel).

    But I don't see how to get the view instance corresponding to the view-model instance.

    In my case I have the SettingsViewModel instance and I need to get the instance of the displayed SettingsFlyout.

    Is there a way?

标签: mvvmcross
1条回答
Bombasti
2楼-- · 2019-08-27 21:55

This is a custom presentation/navigation, so this is left open to the individual app to implement.

Some possible ideas are:

  • the Presenter could keep a list of shown Views (and then get their ViewModels by inspecting their ViewModel / DataContext property) - if doing this, be careful to remove each View from this list when they are closed otherwise this could cause horrible memory leaks.
  • the SettingsFlyout class could itself just have a Current static member - or could call a singleton service which stores the Current (if needed this could be archicted into a more general solution - beyond just the SettingsFlyout)
  • the Presenter could inspect and search the VisualTree for the appropriate View to close
  • the Closed mechanism could be a custom event or property fired from your SettingsFlyoutViewModel to your SettingsFlyoutView - instead of using a NavigationHint.
查看更多
登录 后发表回答