问题
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)orClose(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?
回答1:
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/DataContextproperty) - if doing this, be careful to remove each View from this list when they are closed otherwise this could cause horrible memory leaks. - the
SettingsFlyoutclass could itself just have aCurrentstatic member - or could call a singleton service which stores theCurrent(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
Closedmechanism could be a custom event or property fired from your SettingsFlyoutViewModel to your SettingsFlyoutView - instead of using a NavigationHint.
来源:https://stackoverflow.com/questions/20173382/handling-mvxviewmodelclose-when-using-custom-view-presenters