【问题标题】:Reuse controls inside a usercontrol重用用户控件中的控件
【发布时间】:2010-04-08 07:06:57
【问题描述】:

我在 UserControl1 中有一个 UserControl UserControl1 和一个按钮。我有一个 UserControl1ViewModel,它有一个按钮的 ICommand 属性。使用此命令,我需要在 VM 外部(从其他 VM 或 MainWindow 的 VM)调用方法。对此的最佳做法是什么?

【问题讨论】:

    标签: wpf silverlight mvvm icommand relaycommand


    【解决方案1】:

    您可能需要检查 Laurent Bugnion http://www.galasoft.ch/mvvm/getstarted/ 的 MVVM lite

    这是一个用于帮助实施 mvvm 概念的轻量级工具包。其中,每个视图模型都是 ViewModelLocator 类中的静态成员。例如,在你的命令中你可以做这样的事情。

    ViewModelLocator.MainViewModel.MainContent = NewContent;
    

    您完全可以在没有 mvvm lite 的情况下做到这一点,但使用它确实有助于加快学习曲线并加强模块化。

    【讨论】:

      【解决方案2】:

      您很可能希望实现 Mediator 模式来处理两个视图模型之间的通信。

      另一个类似的问题是: mvvm-view-model-view-model-communications

      【讨论】:

      • 我了解中介者模式。但我对如何允许命令的外部处理程序感到困惑。
      【解决方案3】:

      我会考虑使用控制器作为 ViewModel 之间的中介。 The WPF Application Framework (WAF) 展示了它是如何工作的。

      【讨论】:

        猜你喜欢
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-23
        • 2022-10-05
        • 1970-01-01
        • 2015-02-26
        相关资源
        最近更新 更多