活动而不是命令的MVVM? [英] Events rather than Commands in MVVM?
问题描述
它经常在MVVM的各种教程指定,即MVVM的目标不是消除$ C $的c-背后,一些事件处理可能仍然是必要的在$ C $的c-后面。
It is often specified in the various tutorials of MVVM, that the goal of MVVM is not to eliminate the code-behind and that some event-handling may still be necessary in the code-behind.
什么是你需要写在$ C $事件C-后面,而不是在视图模型用命令的情况?
What are the scenarios in which you need to write events in code-behind rather than using commands in viewmodel?
推荐答案
在一般情况下,如果你的code属于UI逻辑,保持在视图的XAML或code-后面。视图模型只负责桥接和视图和模型之间的结合数据。
In general, if your code pertains to UI logic, keep it in the view's XAML or code-behind. The view model is only responsible for bridging and binding data between the view and the model.
这是例子可以在我的问题之一发现,<一个href="http://stackoverflow.com/questions/5493149/how-do-i-make-a-wpf-window-movable-by-dragging-the-extended-glass-frame">How我做一个WPF窗口移动拖动扩展玻璃框架?的一个我用事件是 SourceInitialized
,在我访问窗口
的窗口句柄来执行一些Windows API的魔力。但是,这一切涉及到窗口,并有无关超出了窗口应用程序逻辑,所以我限制它的所有窗口的code-隐藏文件,使视图模型完全不知道这一点。
An example can be found in one of my questions, How do I make a WPF window movable by dragging the extended glass frame? One of the events I use is SourceInitialized
, in which I access the Window
's window handle to perform some Windows API magic. But all this relates to the window, and has nothing to do with application logic beyond the window, so I restrict it all to the window's code-behind file, leaving the view model completely ignorant of it.
这篇关于活动而不是命令的MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!