MVVM 基础:如何从 ViewModel 关闭应用程序窗口
我正在使用 MVVM Foundation 但我认为它非常简单,并且不是真正特定于框架的。我的设置如下:
StartViewModel - 有一个返回 RelayCommand/ICommand 的 ExitCommand
public ICommand ExitCommand { 获取 { 返回 _exitCommand ?? (_exitCommand = new RelayCommand(() => MessageBox.Show("Hello World"))); } } 公共RelayCommand_exitCommand;
StartView(用户控件)有一个绑定到 ExitCommand 的按钮
I am using MVVM Foundation but I think its quite straight-forward and not really framework specific. My setup is as follows:
StartViewModel - has a ExitCommand that returns a RelayCommand/ICommand
public ICommand ExitCommand { get { return _exitCommand ?? (_exitCommand = new RelayCommand(() => MessageBox.Show("Hello World"))); } } public RelayCommand _exitCommand;
StartView (User Control) has a button binded to the ExitCommand
<Button Content="Exit" Command="{Binding ExitCommand}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,尽可能多地阅读 MVVM,例如 WPF 应用程序与模型 - MSDN 上的视图-视图模型设计模式。一旦您了解了驱动它的基本原理,答案就会显得更加合理。
基本上,您希望将 View (UI) 和 ViewModel(本质上是抽象 UI,但也是抽象 Model)层分开< /a> 并解耦。显示消息框或关闭窗口应被视为 UI 特定细节,因此在视图中实现,或者在消息框的情况下,更普遍地通过“服务”实现。
对于 ViewModel,这是通过使用控制反转 (IoC) 来实现的。以上面的消息框为例。它不是显示消息框本身,而是依赖于 IMessageBoxService,该服务具有 Show 方法,并且 ViewModel 会调用该方法 - 委派责任。这可以通过利用依赖注入 (DI) 容器来进一步实现。
用于关闭视图窗口的另一种方法可能是让 ViewModel 公开视图订阅的事件,例如称为 RequestClose(如 MSDN 文章中所述)。然后ViewModel会在想要关闭相应的View/窗口时引发该事件;它假设其他东西正在倾听并且会承担责任并实际去做。
First, read as much as you can stomach on MVVM, e.g. WPF Apps With The Model-View-ViewModel Design Pattern on MSDN. Once you understand the basic principles driving it the answer will seem more reasonable.
Basically you want to keep your View (UI) and ViewModel (essentially abstract UI, but also abstract Model) layers separate and decoupled. Showing a message box or closing a window should be considered a UI specific detail and therefore implemented in the View, or in the case of a message box, more generally available via a 'Service'.
With respect to the ViewModel, this is achieved using Inversion of Control (IoC). Take the message box example above. Rather than showing the message box itself, it takes a dependency on an IMessageBoxService which has a Show method and the ViewModel calls that instead - delegating responsibility. This could be taken further by leveraging Dependency Injection (DI) containers.
Another approach used for closing a View window might be for the ViewModel to expose an event, called for example RequestClose (as in the MSDN article), that the View subscribes to. Then the ViewModel would raise the event when it wants the corresponding View / window to close; it assumes something else is listening and will take responsibility and actually do it.
您可以在 StartViewModel 中实现 CloseEvent。在您的 StartView 中,您必须注册此 CloseEvent。当您从虚拟机中引发 closeevent 时,您的视图会识别出它必须关闭您的应用程序/窗口。
you can implement an CloseEvent in your StartViewModel. in your StartView you have to register this CloseEvent. when you Raise your closeevent from your VM then, your View recognize that it has to close your app/window.