Handling MvxViewModel::Close when using custom view presenters

送分小仙女□ 提交于 2019-12-11 20:26:44

问题


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?


回答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 / 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.


来源:https://stackoverflow.com/questions/20173382/handling-mvxviewmodelclose-when-using-custom-view-presenters

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