WPF命令(ICommand)

首先创建一个MyCommand类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WPFTest
{
    public class Mycommand : ICommand
      
    {
       //创建委托类型,用于后面接收构造函数里面的参数
        Action executeAction;
        public Mycommand(Action action)
        {
            executeAction = action;
        }
        public event EventHandler CanExecuteChanged;
        //允许执行
        public bool CanExecute(object parameter)
        {
            return true;
        }
        //把Mycommand接收的委托参数用于执行
        public void Execute(object parameter)
        {
            executeAction();
        }
    }
}

MainViewMode

主要是写业务的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WPFTest
{
   public class MainViewModel

    {
        public MainViewModel()
        {
            //刚好需要一个委托参数,把Show方法传进去
            ShowCommand = new Mycommand(Show);

        }
     //负责UI与业务之间建立联系
        public Mycommand ShowCommand { get; set; }
        public void Show()
        {
            MessageBox.Show("点击了按钮!");
        }
    }
}

View

<Window x:Class="WPFTest.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:WPFTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Command="{Binding ShowCommand}"/>
    </Grid>
</Window>

MainWindow

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 WPFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            //整个MainViewModel作为数据源传给xmal
            this.DataContext = new MainViewModel();
    
        }
}

}

A:WPF中的ICommand是一个接口,用于封装在MVVM(Model-View-ViewModel)架构中的命令行为。ICommand包含三个方法:Execute,CanExecute和CanExecuteChanged事件。Execute方法表示命令的执行操作,CanExecute方法用于检查命令是否可以执行,CanExecuteChanged事件在CanExecute方法的返回值发生变化时发生。 以下是一个示例命令类,实现了ICommand接口: ```C# public class RelayCommand : ICommand { private readonly Action<object> execute; private readonly Predicate<object> canExecute; public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public RelayCommand(Action<object> execute, Predicate<object> canExecute = null) { this.execute = execute ?? throw new ArgumentNullException(nameof(execute)); this.canExecute = canExecute; } public bool CanExecute(object parameter) { return canExecute == null || canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } } ``` 使用此命令类,你可以在ViewModel中声明命令,并在View中绑定到命令上: ```C# public class MyViewModel { public ICommand MyCommand { get; } public MyViewModel() { MyCommand = new RelayCommand(DoSomething, CanDoSomething); } private bool CanDoSomething(object parameter) { // Return true or false to indicate if the command can be executed return true; } private void DoSomething(object parameter) { // Do something here when the command is executed } } ``` 在View中,你可以使用以下代码将命令绑定到Button的Click事件上: ```XAML <Button Command="{Binding MyCommand}" Content="Execute Command" /> ``` 当用户单击按钮时,将触发MyCommand的Execute方法。可以使用CanExecute方法来控制按钮是否应该启用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值