当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How a ViewModel communicates with its own View

问题描述:

I am developing a WPF MVVM Light application, I want to display some message boxes/dialog boxes or there are some other scenarios where I want my View model to communicate with its view or raise some event on its respective view.

How could I achieve that?

网友答案:

See Messenger class of MVVM Light. It should even have sample by default when you downloa MVVM light. It's something like Messenger.Default.Send(new YourMessageClass()) and you can register listener on View side.

网友答案:

The prism framework has a concept of interaction requests to solve this problem. A nice example can be found here or here.

网友答案:

The responsibility of displaying the popup should not be the ViewModel's responsibility, but a "service that takes care of it for you".

If the "popup window" is a modal like window like a "file open dialog", "file save dialog", message box "ok/cancel" or "yes/no" etc, then I believe that a "service" would be the right choice. The reason is that the service can be facked/mocked for unit testing.

Also, you can have different implementations of this service based on whatever you want: in debug (r, when the role is of type admin or developer) you have more "verbose" information than in release or regular users, for example.

Now, for "how does a ViewModel communicate to the View", I'm not sure what you mean. Simply add a property on the ViewModel and bind the view to that property.

分享给朋友:
您可能感兴趣的文章:
随机阅读: