WPF的ICommand接口的简单实现

2 篇文章 0 订阅
  • WPF的ICommand接口的简单实现
using System; 
using System.Windows.Input;
namespace WpfTest { 
	class DelegateCommand : ICommand {
		public Action _action; 
 		public event EventHandler CanExecuteChanged; 
 		public DelegateCommand(Action action) 
 		{
 		 _action = action; 
 		}
 		public bool CanExecute(object parameter)//执行检查是否可以执行 
 		{
 		 return true; 
 		}
 		public void Execute(object parameter)//执行 { _action(); 
 		{
 		//...
 		}
  } 
}
  • 这是最简单的实现
  • 在CanExcute方法里直接返回了True,没有进行判断
  • 没有传输和使用方法里面的参数
  • 方法没有使用泛型

以下是配套的测试代码
  • XAML代码
  • 在界面有一个Button,并Binding了Command
<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="350" width="525">
 <grid> 
 	<button width="100" height="50" content="Button" command="{Binding ButtonCommand}">
 	</button> 
 	</grid> 
</window>
  • Main连接ViewModel
using System.Windows;
namespace WpfTest { 
/// <summary> ///
MainWindow.xaml 的交互逻辑 
/// </summary> 
public partial class MainWindow : Window 
{
	public MainWindow() 
	{
		InitializeComponent(); 
		this.DataContext = new MainViewModel(); //View和ViewMode连接
	}
}
}
  • ViewModel代码
using System.Windows.Input; 
namespace WpfTest {
	class MainViewModel : ViewModelBase 
	{
	 public string ButtonOperation { get; set; } 
	 public ICommand ButtonCommand { get; set; } 
	 public MainViewModel() 
	 {
	 	 ButtonCommand = new DelegateCommand(ButtonCommandHandler); 
	 }
	 private void ButtonCommandHandler() 
	 { 
	 ButtonOperation = "Finish"; 
	 } 
	 }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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" /> ``` 当用户单击按钮时,将触发MyCommandExecute方法。可以使用CanExecute方法来控制按钮是否应该启用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值