如何使用ViewModelCloser关闭ViewModel的视图? [英] How to use ViewModelCloser to close the View of the ViewModel?
问题描述
在MvvmCross v3(客户管理)示例中,方法void RequestClose(IMvxViewModel viewModel)
关闭顶部的View
.您如何代替ViewModel
的View
?
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?
推荐答案
我不会使用该ViewModelCloser方法-尽管您可以根据需要扩展它.
I wouldn't use that ViewModelCloser method - although it could be extended if you want to.
MvvmCross v3删除了以前的CloseViewModel
方法-因为它不能在所有平台和所有演示样式上都真正起作用-在所有导航控制器,拆分视图,选项卡,弹出窗口,弹出窗口,对话框等中都可以使用.
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.
要替换它,v3引入了一个新的ViewModel调用:
To replace it, v3 introduces a new ViewModel call:
protected bool ChangePresentation(MvxPresentationHint hint)
这在UI中使用IMvxViewPresenter方法进行了匹配:
This is matched in the UIs with an IMvxViewPresenter method:
void ChangePresentation(MvxPresentationHint hint);
要使用此功能,您需要:
To use this, you will need to:
-
创建一个新的提示类-例如
public class CustomPresentationHint : MvxPresentationHint { /* ... */ }
在每个UI项目中,提供一个自定义演示者(通常通过覆盖Setup.cs
类中的CreateViewPresenter()
)-在该自定义演示者中处理ChangePresentationHint
调用:
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
}
}
在您的视图模型中,您可以在需要时发送CustomPresentationHint
.
我意识到这比vNext所需的工作更多,但希望它是一种更灵活,更强大的方法.
I realise this is 'more work' than was required in vNext, but hopefully it's a more flexible, powerful approach.
这篇关于如何使用ViewModelCloser关闭ViewModel的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!