WPF之命令
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Height="100" Width="100" Content="命令测试" Command="{Binding Path=Test}" CommandParameter="{Binding User}"></Button>
<!--没有事件函数会报错-->
<Button Height="100" Width="100" Content="命令测试" Click="Button_Click"></Button>
<!--可以先定义使用,后写-->
<Button Height="100" Width="100" Content="命令测试" Command="{Binding Path=Test2}" CommandParameter="{Binding User2}"></Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
public class Context
{
/// <summary>
/// 定义属性
/// </summary>
public ICommand Test { get; set; } = new TestCommand();
public User User { get; set; } = new User();
}
/// <summary>
/// 命令的定义(命令是类)
/// </summary>
public class TestCommand : ICommand
{
/// <summary>
/// 状态变更通知事件
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// 判断是否执行
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CanExecute(object parameter)
{
return true;
}
/// <summary>
/// 执行任务
/// </summary>
/// <param name="parameter"></param>
/// <exception cref="NotImplementedException"></exception>
public void Execute(object parameter)
{
User user = parameter as User;
MessageBox.Show("测试"+ user.UserName);
}
}
public class User
{
public string UserName { get; set; } = "111";
public string PassWord { get; set; } = "222";
}
}
在 WPF 中,命令(Command)是一种实现用户界面(UI)与应用程序逻辑分离的机制。通过使用命令,你可以将按钮点击、菜单项选择等用户操作与具体的业务逻辑解耦,从而提高代码的可维护性和可重用性
目的:用来取代事件函数
好处1:与具体的业务逻辑解耦
解耦了,就方便同时开展工作,进行分工合作
好处2:功能性更强:加上了是否执行的判定,还有状态变更通知,界面带参传递功能。
一 定义命令
/// <summary>
/// 命令的定义(命令是类)
/// </summary>
public class TestCommand : ICommand
{
/// <summary>
/// 状态变更通知事件
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// 判断是否执行
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CanExecute(object parameter)
{
return true;
}
/// <summary>
/// 执行任务
/// </summary>
/// <param name="parameter"></param>
/// <exception cref="NotImplementedException"></exception>
public void Execute(object parameter)
{
MessageBox.Show("测试");
}
}
二 使用命令
绑定数据上下文类
public MainWindow()
{
InitializeComponent();
this.DataContext = new Context();
}
绑定控件的命令属性
<Grid>
<Button Height="100" Width="100" Content="命令测试" Command="{Binding Path=Test}"></Button>
</Grid>
三 命令传参
定义属性
public class User
{
public string UserName { get; set; } = "111";
public string PassWord { get; set; } = "222";
}
public class Context
{
/// <summary>
/// 定义属性
/// </summary>
public ICommand Test { get; set; } = new TestCommand();
public User User { get; set; } = new User();
}
<Button Height="100" Width="100" Content="命令测试" Command="{Binding Path=Test}" CommandParameter="{Binding User}"></Button>
在命令中使用
/// <summary>
/// 执行任务
/// </summary>
/// <param name="parameter"></param>
/// <exception cref="NotImplementedException"></exception>
public void Execute(object parameter)
{
User user = parameter as User;
MessageBox.Show("测试"+ user.UserName);
}
四 不用命令,用事件函数进行比较,说明什么是实现用户界面(UI)与应用程序逻辑分离解耦
<!--没有事件函数会报错-->
<Button Height="100" Width="100" Content="命令测试" Click="Button_Click"></Button>
<!--可以先定义使用,后写-->
<Button Height="100" Width="100" Content="命令测试" Command="{Binding Path=Test2}" CommandParameter="{Binding User2}"></Button>