首页 > 其他分享 >ICommand的实现(1)

ICommand的实现(1)

时间:2024-03-22 11:24:27浏览次数:32  
标签:ICommand CanExecute 控件 实现 object command parameter

ICommand 接口在 System.Windows.Input 命名空间内定义。它有两个方法和一个事件。

   //
   // 摘要:
   //     Occurs when changes occur that affect whether or not the command should execute.
   event EventHandler? CanExecuteChanged;

   //
   // 摘要:
   //     Defines the method that determines whether the command can execute in its current
   //     state.
   //
   // 参数:
   //   parameter:
   //     Data used by the command. If the command does not require data to be passed,
   //     this object can be set to null.
   //
   // 返回结果:
   //     true if this command can be executed; otherwise, false.
   bool CanExecute(object? parameter);
   //
   // 摘要:
   //     Defines the method to be called when the command is invoked.
   //
   // 参数:
   //   parameter:
   //     Data used by the command. If the command does not require data to be passed,
   //     this object can be set to null.
   void Execute(object? parameter);

只有当 CanExecute 返回 true 时才会调用 Execute 方法。如果 CanExecute 方法返回 false,则绑定控件将自动禁用。

为了知道 CanExecute 值,请侦听 CanExecuteChanged 事件,该事件可能因传递的参数而异。

ICommand 接口一般用在 MVVM 架构中,在控件中使用Command命令属性,绑定一个自定义命令“OpenCommand”,由于 OpenCommand只不过是一个 ICommand 实例,因此在加载窗口时,它将检查 CanExecute 返回值,如果它返回 true,则它将启用按钮控件并且 Execute 方法已准备好使用,否则按钮控件将被禁用。控件在使用时,可以使用CommandParameter传递参数。

<StackPanel>
    <Button Margin="10" Command="{Binding OpenCommand}" CommandParameter="ViewA">模块A</Button>
    <Button Margin="10" Command="{Binding OpenCommand}" CommandParameter="ViewB">模块B</Button>
    <Button Margin="10" Command="{Binding OpenCommand}" CommandParameter="ViewC">模块C</Button>
</StackPanel>

ICommand具体的实现类

public class MyComand : ICommand
{
    private readonly Action<object> _action;
    private readonly Func<object,bool>? _func;

    public  MyComand(Action<object> action, Func<object, bool> func)
    {
        _action = action;
        _func = func;
    }

    /// <summary>
    /// 事件处理器
    /// </summary>
    public event EventHandler? CanExecuteChanged;

    /// <summary>
    /// 能否执行 true/false
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool CanExecute(object? parameter)
    {
        return _func(parameter);
    }
    /// <summary>
    /// 执行命令
    /// </summary>
    /// <param name="parameter"></param>
    /// <exception cref="NotImplementedException"></exception>
    public void Execute(object? parameter)
    {
        _action(parameter);
    }
}
View Code

在上述代码中,使用了Action和Func委托,在调用MyCommand的时候,我们需要将方法传递给Action委托,然后再执行ICommand中的Execute。关于为什么这里要使用委托,参见C#的基础知识委托的基本用法,以及学习C#的几个不同的内置委托。

 

标签:ICommand,CanExecute,控件,实现,object,command,parameter
From: https://www.cnblogs.com/xwzyac/p/18089037

相关文章

  • fastendpoint+maomi的一种实现原理
    1整个框架就是fastendpoint(api处理,鉴权授权,参数校验,对象映射等基础功能集成),maoni(Service注入,依赖关系处理,参考的是abp,比较轻量级,源码我放在附件里了,实现模块化注入)fastendpoint:https://fast-endpoints.com/maomi: https://maomi.whuanle.cn/1.module.html2项目截图:......
  • 利用EasyPoi 实现 传入List数据,输出excel文件
    基本描述场景用户传入List数据,要求生成Excel文件(糟糕的需求是真糟糕!!!)本次算是未完成版[应付需求还是可以的](需要硬代码去编写模板,各位宝子们先将就下,后续会跟新传参版)特别提醒时间字段我们当做字符处理的写模板的时候不要用format属性(暂无特别好的解决方案,有大神可以以指......
  • 基于GD32F303,CMSIS-DSP支持包,实现FFT,得到频率,还原单一频率的波形
        一般情况下M33M4的内核是支持DSP包的,用户只需要自己添加支持包,并添加相应的头文件即可,比如#include"arm_math.h",#include"arm_const_structs.h"等等。(1)main.c#include"gd32f30x.h"#include"stdio.h"#include"string.h"#include"arm_......
  • 基于security-oauth2-autoconfigure实现的OAuth2迁移到更现代的解决方案,Spring Securi
    目录OAuth2资源服务器配置步骤1:添加依赖步骤2:配置资源服务器OAuth2客户端配置(可选)/**其他应用作为OAuth2客户端步骤1:添加依赖步骤2:配置OAuth2.0客户端/**应用同时作为OAuth2客户端步骤1:配置OAuth2.0客户端控制器示例结合使用OAuth2与JWT        ......
  • 自定义转换器实现案例
    Spring中自定义转换器实现案例1自定义转换器code如下:packagecom.gientech.selfConvert;importorg.springframework.core.convert.converter.Converter;publicclassStudentConverterimplementsConverter<String,Student>{@OverridepublicStudentconv......
  • 基于Springboot的瑜伽馆管理系统的设计与实现(有报告)。Javaee项目,springboot项目。
    演示视频:基于Springboot的瑜伽馆管理系统的设计与实现(有报告)。Javaee项目,springboot项目。项目介绍:采用M(model)V(view)C(controller)三层体系结构,通过Spring+SpringBoot+Mybatis+Vue+Maven+Layui+Elementui来实现。MySQL数据库作为系统数据储存平台,实现了基于B/S结构......
  • C语言实现反转整数
    题目描述:从标准输入流(控制台)中获取一个整数 num,将其 按位反转 后通过输出语句输出反转后的整数,保留原来整数的正负性。思路:前提是num不等于0首先我们需要定义一个中间变量 temp 来存放当前 num 的最小位,获取最小位存在temp---temp=num%10通过 result=result*1......
  • AJAX 前端开发利器:实现网页动态更新的核心技术
    AJAXAJAX是开发者的梦想,因为你可以:在不重新加载页面的情况下更新网页在页面加载后请求来自服务器的数据在页面加载后接收来自服务器的数据在后台向服务器发送数据HTML页面<!DOCTYPEhtml><html><body><divid="demo"><h2>让AJAX更改这段文字</h2><buttontype=......
  • 哈希表及其实现
    哈希概念        顺序结构以及平衡树中,元素关键码与其存储位置之间没有对应的关系,因此在查找一个元素时,必须要经过关键码的多次比较。顺序查找时间复杂度为O(N),平衡树中为树的高度,即O(log2N),搜索的效率取决于搜索过程中元素的比较次数。        哈希方法:可......
  • 限流器(流控)+ 线程 C++实现
    在C++中,你可以使用互斥锁(mutex)和条件变量(conditionvariable)来实现一个简单的限流器(流控)以及线程。下面是一个简单的例子,它创建了一个限流器类,该类允许一定数量的线程同时访问某个资源。#include<iostream>#include<thread>#include<mutex>#include<condition_variable>......