[英]When to use event and command for WPF/MVVM?
I am practicing on how to write a WPF application with MVVM pattern. 我正在练习如何使用MVVM模式编写WPF应用程序。 So far I haven't used command in my code.
到目前为止,我还没有在代码中使用命令。 In my Viewmodel I implement
INotifyPropertyChanged
and used (event PropertyChangedEventHandler PropertyChanged
) to fire events. 在我的Viewmodel中,我实现
INotifyPropertyChanged
并用于触发(事件PropertyChangedEventHandler PropertyChanged
)事件。 Why do I feel like I still miss some concept of WPF about how to use command? 为什么我仍然想念WPF有关如何使用命令的某些概念?
When is it appropriate to use commands? 什么时候适合使用命令?
Commands in WPF are used to abstract an user-triggered action (such as clicking a Button
or pressing a key. WPF中的命令用于抽象用户触发的操作(例如单击“
Button
或按某个键)。
here's a basic example: 这是一个基本示例:
Suppose you want to search for employees in your database when the user clicks the "Search" button, or hits the enter key while focusing the Search Box. 假设您要在用户单击“搜索”按钮或在聚焦搜索框时按Enter键时在数据库中搜索员工。
You might define your ViewModel like this: 您可以这样定义ViewModel:
public class MyViewModel: ViewModelBase
{
public string SearchText {get;set;} //NotifyPropertyChanged, etc.
public Command SearchCommand {get;set;}
public MyViewModel()
{
//Instantiate command
SearchCommand = new DelegateCommand(OnSearch);
}
private void OnSearch()
{
//... Execute search based on SearchText
}
}
And your View: 和您的观点:
<StackPanel>
<TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding SearchCommand}"/>
</TextBox.InputBindings>
</TextBox>
<Button Content="Search" Command="{Binding SearchCommand}"/>
</StackPanel>
Notice how the KeyBinding
and the Button's Command
property are both bound to the same Command ( SearchCommand
) in the ViewModel. 注意,如何将
KeyBinding
和Button的Command
属性都绑定到ViewModel中的同一Command( SearchCommand
)。 This facilitates reutilization, and also helps keep actual logic in the ViewModel, with all its goodness (testability, etc), while keeping the view clean and code-less. 这有利于重用,还有助于将ViewModel的实际逻辑及其所有优点(可测试性等)保留在ViewModel中,同时保持视图整洁且无需编写代码。
The fun thing is that using original Commands concept from WPF is absolutely not required :). 有趣的是,绝对不需要使用WPF中的原始Commands概念:)。 You could build large and complex applications with all that beauty of loosely coupled design (xaml) and business logic (c#/vb code) in place with just using MVVM all around and free open source application framework library Caliburn.Micro .
您可以使用大型MVVM和免费的开源应用程序框架库Caliburn.Micro来充分利用松耦合设计(xaml)和业务逻辑(c#/ vb代码)的美感来构建大型复杂的应用程序。
Disclaimer : I'm just a happy user of this library and have nothing to do with its creators, so this is not paid ads or something like that. 免责声明 :我只是这个库的快乐用户,并且与它的创建者没有任何关系,因此这不是付费广告或类似的东西。
Please, just take a look over this very basic sample from official documentation : 请看一下官方文档中的这个非常基本的示例 :
--> Basic Configuration, Actions and Conventions <-- -> 基本配置,操作和约定 <-
and you will fill the power of binding events from you XAML view directly to methods in your C# view-model without mess of proxy code for commands declaration and registration (like this is implemented in other similar application frameworks). 这样,您就可以将XAML视图中的事件直接绑定到C#视图模型中的方法中,而不会给命令声明和注册带来麻烦(例如在其他类似的应用程序框架中实现)代理代码。
And never mind that this sample is Silverligh app - Caliburn.Micro support all major Xaml platforms almost the same way and the WPF sample will looks pretty like above Silverlight-based. 没关系,此示例是Silverligh应用程序-Caliburn.Micro几乎以相同的方式支持所有主要的Xaml平台 ,并且WPF示例与基于Silverlight的外观非常相似。
In addition to the mentioned major capability (buinding to methods) Caliburn.Micro have: 除了提到的主要功能(基于方法)之外,Caliburn.Micro还具有:
Just give it the chance and you will never need vanilla WPF Commands ;) 只要给它机会,您就不再需要香草WPF命令;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.