WPF MVVM取消窗口关闭 [英] WPF MVVM cancel window closing
问题描述
我是新来WPF和MVVM。我想最小化窗口关闭它代替。换句话说,我想取消关闭窗口
事件,并尽量减少此窗口。
I am new to WPF and MVVM. I would like to minimize the window instead of closing it. In other words, I would like to cancel Closing
event of window and minimize this window.
我应该怎么做的MVVM方式?
How should I do that MVVM way?
如果是相关的,最后我将设置 ShowInTaskbar
到假
和使用的WinForms盘组件。
If it's relevant, at the end I will set ShowInTaskbar
to false
and use WinForms tray component.
推荐答案
常见的误解MVVM是,有可能永远,永远是code隐藏在视图中。这是不正确的。
The common misunderstanding with MVVM is that there can never, ever be code-behind in a view. That is simply not true.
MVVM的目标是减少的的code在code-落后,但事情直接与视图本身(如Windows事件)进行交互,<强>这是可以接受的把一些code-背后。在code-后面会处理取消,并可以做到最小化,或者叫在视图模型的命令,或其他类似的东西。
The goal of MVVM is to minimize the code in the code-behind, but for things that directly interact with the view itself (such as Windows events), it is acceptable to put in some code-behind. The code-behind would handle the Cancel, and may do the minimize, or call a command in the ViewModel, or some other such thing.
否则,你将不得不拿出处理事件的视图模型,它通过具有视图模型打破MVVM模式一个令人费解的系统有一个参考视图(而不是周围的其他方法)。
Otherwise, you are going to have to come up with a convoluted system of handling the event in the ViewModel, which breaks the MVVM pattern by having the ViewModel have a reference to the View (instead of the other way around).
这篇关于WPF MVVM取消窗口关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!