问题描述
我如何使用MVVM灯相互通信两个视图模型.我知道如何使用Messenger课程和注册等.这是我的场景
A Settings View ---> a Settings View Model . . . A MainPage View ---> A MainPage ViewModel
如果设置视图中的某些东西会发生变化,它将回到设置视图模型.因此,我希望"设置视图"模型可以与Mainpage视图模型传达有关发生的变化的模型.然后,Mainpage ViewModel将说明视图.
推荐答案
这种问题样式的一种常见模式是 sediator (两者都可以查看模型参考,可用于传递两者之间的消息).
此后,调解人类已移至 cinch wpf/sl mvvm框架被积极开发/支持.
我更喜欢的模式是事件聚集器,可以在Prism框架中找到一个示例.在这种模式下,不同的视图模型订阅了聚合器和其他事件的事件.
希望这会有所帮助
其他推荐答案
我第二奈杰尔关于使用调解人的建议,看看乔什·史密斯的博客及其实施:
http://joshsmithonwpf.wordpress.com/?s = mediator p>
在底部,您可以下载调解器原型和演示,请记住将其从.doc重命名为.zip.
希望这会有所帮助...
其他推荐答案
您可以尝试的一件事是尝试实现依赖性反转. 定义一些操作/合同的接口. 在MainViewModel上实现该接口. 将该接口作为成员变量传递到settingsViewModel. 因此,每当设置视图模型必须通知主要内容时,它将使用该接口. 此外,其他视图模型可以使用相同的策略.
public interface IMessenger { void NotifyAction(); }public class MainViewModel:InotifyProprtyChanged,IMessenger { public void NotifyAction() { } } public class SettingsViewModel:INotifyPropertyChanged { public IMessenger Messenger{get;set;} public void SomeCommandExecutor() { if(Messenger!=null) { Messenger.NotifyAction(); } } }
问题描述
How do I go about having two view models communicate with one another using MVVM Light. I know how to use the messenger class and register etc.. Here is my Scenario
A Settings View ---> a Settings View Model . . . A MainPage View ---> A MainPage ViewModel
If something changes in the Settings View it will Message back to the Settings View Model. So then I want the Settings View Model to communicate to the MainPage View Model about what changed. THe MainPage ViewModel will then tell the View.
推荐答案
A common pattern for this style of problem is Mediator (a class that both view models reference and can be used to pass messages between the two).
The Mediator class has since been moved to the Cinch WPF/SL MVVM Framework, which appears to still be actively developed/supported.
The pattern I prefer is the Event Aggregator, an example can be found in the Prism framework. In this pattern different view models subscribe to events from the aggregator and others publish events.
Hope this helps
其他推荐答案
I second Nigel's suggestion of using the Mediator, take a look at Josh Smith's blog and his implementation of this:
http://joshsmithonwpf.wordpress.com/?s=mediator
At the bottom you can download the Mediator Prototype and Demo, just remember to rename it from .doc to a .zip.
Hope this helps...
其他推荐答案
One thing you can try out is try to implement Dependency Inversion. Define an interface with some actions/contracts. Implement that interface on MainviewModel. Pass that interface as member variable to SettingsViewModel. So whenever settings view model has to notify something to main, it will use that interface. And additionally, other view models can use same strategy.
public interface IMessenger { void NotifyAction(); }public class MainViewModel:InotifyProprtyChanged,IMessenger { public void NotifyAction() { } } public class SettingsViewModel:INotifyPropertyChanged { public IMessenger Messenger{get;set;} public void SomeCommandExecutor() { if(Messenger!=null) { Messenger.NotifyAction(); } } }