WPF COMMAND在Windows Presentation Foundation(WPF)框架中是一个设计模式,主要用于实现用户界面(UI)元素和业务逻辑之间的松耦合交互。具体来说,它是MVVM(Model-View-ViewModel)架构中的重要组成部分。
在WPF中,Command是一种行为,允许UI控件(如按钮)的Command属性绑定到一个命令对象,而不是直接绑定到一个方法调用。当用户触发UI事件时(例如点击按钮),相应的命令会被执行,并且该命令可以关联到任何实现了 ICommand 接口的对象,这个对象定义了何时命令可执行以及执行命令时应进行的操作。
通过这种方式,开发者可以在ViewModel层中定义命令并处理业务逻辑,然后在View层中仅声明要绑定的命令,从而极大地增强了代码的可维护性和可测试性。
在WPF应用中,可以通过数据绑定将ICommand实例与UI控件(如Button)的Command属性关联起来,控件的Command属性绑定的主要是方法,这样在用户与UI交互时,就能够自动调用对应的命令执行逻辑,而无需直接在控件的事件处理器中写入复杂的业务代码。
不管是ICommand还是INotifyPropertyChanged
都必须首先将ViewMode的实例设置为控件或整个界面的 DataContext如,
this.DataContext = new MainViewModel();DataContext是UI层与数据逻辑层的桥梁。
ICommand 接口中定义了两个方法和一个属性:
-
Execute
方法:当命令被调用时执行的方法,通常在这里编写处理实际业务逻辑的代码。void Execute(object parameter);
-
CanExecute
方法:判断命令当前是否可以被执行。这个方法返回一个布尔值,视图会根据这个值决定是否使能关联的UI控件。bool CanExecute(object parameter);
-
CanExecuteChanged
事件:当命令的可执行状态改变时触发此事件,视图接收到此事件后会重新检查并更新相关UI控件的状态。event EventHandler CanExecuteChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace WpfApplication1
{public class MainViewModel{public MainViewModel(){ShowMyCommand = new MyCommand(show);}public MyCommand ShowMyCommand{set;get;}public void show(){MessageBox.Show("按钮2被点击了");}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApplication1
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainViewModel();//MainViewModel作为数据源为MainWindow提供上下文}private void Button_Click(object sender, RoutedEventArgs e){MessageBox.Show("按钮被点击了");}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfApplication1
{public class MyCommand : ICommand{public Action ExecuteAction;public MyCommand(Action act){ExecuteAction += act;}public event EventHandler CanExecuteChanged;public bool CanExecute(object parameter){return true;}public void Execute(object parameter){ExecuteAction();}}
}
<Window x:Class="WpfApplication1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApplication1"mc:Ignorable="d"Title="MainWindow" Height="350" Width="525"><Grid><StackPanel><Button Content="按钮" Click="Button_Click"/> <!--以事件方式触发--><Button Content="按钮2" Name="btnCommand" Command="{Binding ShowMyCommand}"/><!--已绑定方式实现--></StackPanel></Grid>
</Window>