WPF:关闭从模型MVVM一个窗口 [英] WPF: Close a Window from Model MVVM

查看:187
本文介绍了WPF:关闭从模型MVVM一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图关闭其视图模型的一个窗口。我使用MVVM模式。我已经累到让使用窗口;

I am trying to close a window from its ViewModel. I am using the MVVM pattern. I have tired to get the window using;

Window parentWindow = Window.GetWindow(this);

但我不能做到这一点,我怎么得到视图模型的窗口,使我能够关闭窗口。我希望能够做到这一点在code。

But I cannot do this, how do I get the window of the ViewModel so I am able to close the window. I want to be able to do this in code.

您可以在code中的父窗口?

Can you find the parent window in code?

推荐答案

的ViewModels 不应该被引用查看以任何方式,包括关闭窗户,在MVVM。

ViewModels should not be referencing the View in any way, including closing windows, in MVVM.

事实上,国与国之间的通信查看视图模型通常是通过某种事件或消息系统中,做过这样作为微软棱镜的 EventAggregator MVVM光的使者

Instead, communication between the View and ViewModel is typically done through some kind of Event or Messaging System, such as Microsoft Prism's EventAggregator, or MVVM Light's Messenger

例如,查看应该订阅监听类型的事件消息 CloseWindow ,当它收到一个这些消息也应该关闭自己。那么视图模型只是有它要告诉查看<α CloseWindow 信息随时播放/ code>关闭。

For example, the View should subscribe to listen for event messages of type CloseWindow, and when it receives one of those message it should close itself. Then the ViewModel simply has to broadcast a CloseWindow message anytime it wants to tell the View to close.

有事件系统在MVVM,并举例简要介绍,对有关的的ViewModels之间的通信如果你有兴趣

There's a brief overview of event systems in MVVM, and some examples, on my blog post about Communication between ViewModels if you're interested

这篇关于WPF:关闭从模型MVVM一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆