【问题标题】:How to use ViewModelCloser to close the View of the ViewModel?如何使用 ViewModelCloser 关闭 ViewModel 的 View?
【发布时间】:2013-04-10 07:12:43
【问题描述】:

在 MvvmCross v3,CustomerManagement 示例中,方法 void RequestClose(IMvxViewModel viewModel) 关闭顶部 View。您如何关闭 ViewViewModel 而不是?

【问题讨论】:

    标签: c# xamarin mvvmcross


    【解决方案1】:

    我不会使用那个 ViewModelCloser 方法 - 尽管如果你愿意,它可以被扩展。

    MvvmCross v3 删除了之前的 CloseViewModel 方法 - 因为它并没有真正适用于所有平台和所有演示样式 - 所有导航控制器、拆分视图、选项卡、弹出窗口、弹出窗口、对话框等。

    为了替换它,v3 引入了一个新的 ViewModel 调用:

        protected bool ChangePresentation(MvxPresentationHint hint)
    

    这在 UI 中与 IMvxViewPresenter 方法匹配:

        void ChangePresentation(MvxPresentationHint hint);
    

    要使用它,您需要:

    1. 创建一个新的 Hint 类 - 例如public class CustomPresentationHint : MvxPresentationHint { /* ... */ }

    2. 在每个 UI 项目中,提供一个自定义演示者(通常通过覆盖 Setup.cs 类中的 CreateViewPresenter()) - 并在该自定义演示者中处理 ChangePresentationHint 调用:

            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. 在您的视图模型中,您可以随时发送CustomPresentationHint

    我意识到这比 vNext 所需的“工作量更大”,但希望这是一种更灵活、更强大的方法。

    【讨论】:

    • 我看过 ChangePresentation(MvxPresentationHint hint),我猜这个提示可以存储 MvxBundle 任何信息,但是,问题是我在 ViewModel 中调用 ChangePresentation,我不知道如何传递 ViewModel与演示者相关的视图。
    • 这是你的代码——你可以做你想做的——例如您可以将 public IMvxViewModel ViewModelToClose {get;set; 属性添加到您的 CusomPresentationHint
    • CustomPresentationHint 提示 = new CustomPresentationHint();提示.ViewModelToClose = 这个; ChangePresentation(提示); Presenter只拿到viewmodel,我怎么找到对应的View
    • 这取决于视图的呈现方式。演示者拥有视图 - 每个视图都通过演示者中的 ShowView 显示。
    • 我想我只是在presenter中保留一个视图-viewmodel映射
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多