首页 > 其他分享 >WPF implement ICommand and similar with DelegateCommand of Prism

WPF implement ICommand and similar with DelegateCommand of Prism

时间:2024-05-25 21:12:49浏览次数:9  
标签:ICommand executeValue DelegateCommand private Prism RequerySuggested CommandMana

 public class DelCmd : ICommand
 {
     public event EventHandler CanExecuteChanged
     {
         add
         {
             CommandManager.RequerySuggested += value;
         }
         remove
         {
             CommandManager.RequerySuggested -= value;
         }
     }

     private Action<object> _execute;
     private Predicate<object> _canExecute;

     public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue)
     {
         _execute = executeValue;
         _canExecute = canExecuteValue;
     }

     public DelCmd(Action<object> executeValue) : this(executeValue, null)
     {
         _execute = executeValue;
     }

     public bool CanExecute(object parameter)
     {
         if (_canExecute == null)
         {
             return true;
         }
         return _canExecute(parameter);
     }

     public void Execute(object parameter)
     {
         _execute(parameter);
     }
 }
private DelCmd clearCmd;
public DelCmd ClearCmd
{
    get
    {
        if(clearCmd==null)
        {
            clearCmd = new DelCmd(ClearCmdExecuted, ClearCmdCanExecute);
        }
        return clearCmd;
    }
}

private bool ClearCmdCanExecute(object obj)
{
    return !string.IsNullOrWhiteSpace(_imagePath) && System.IO.File.Exists(_imagePath);
}

private void ClearCmdExecuted(object obj)
{
    ImagePath = string.Empty;
}

 

WPF raises the CommandManager.RequerySuggested static event when it thinks the execution state of commands may change. This is typically done after some UI actions that may cause something to change in all bound commands. The following implementation causes WPF to call register for its own notification of re-executing CanExecute for all commands that raise the CanExecuteChange event:

public event EventHandler CanExecuteChanged {
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

 

标签:ICommand,executeValue,DelegateCommand,private,Prism,RequerySuggested,CommandMana
From: https://www.cnblogs.com/Fred1987/p/18212998

相关文章

  • 01_WPF+Prism登录之PasswordBox的Binding
    #region登录信息///<summary>///密码///</summary>privatestring_Pwd;///<summary>///密码///</summary>publicstringPwd{get{return_Pwd;}......
  • WPF & Prism
    WPF编程-Prism世有伯乐,然后有千里马。千里马常有,而伯乐不常有。一、背景Winform和WPF1.WinForms和WPF技术架构:WinForms是基于传统的窗体和控件的技术,使用的是类似于VB6时代的设计理念。WPF是基于XAML(可扩展应用程序标记语言)的技术,允许更灵活、高度可定制化......
  • 记录一次Prism报错
    前提:我的WPF程序在进入主程序之前显示登录界面此时不想登录,点击退出的时候报错了报错提示Anexceptionoccurredwhileinitializingmodule'ShellModule'.-Theexceptionmessagewas:Anexceptionhasoccurredwhiletryingtoaddaviewtoregion'AsideRegion'......
  • mongo prisma
    prisma要求mongo服务必须是集群,需要有副本集执行事务。本地开发环境安装了一个debian12虚拟机:在debian12系统中, 安装docker,docker-compose下载mongo镜像:dockerpullmongo创建配置文件docker-compose.yml:version:'3'services:mongo:container_name:mong......
  • 使用 Prisma ORM 和 Cloudflare D1 构建应用程序
    自2017年推出Workers以来,Cloudflare一直引领边缘计算领域。Cloudflare最近通过D1发布了本机无服务器数据库。我们现在可以使用PrismaORM与D1一起构建应用程序。将数据库部署到边缘CloudflareWorkers是一种分布在全球范围内的轻量级无服务器计算形式。它们......
  • Prism中Region的静态使用
    在Prism框架中如何使用Region(区域)呢?1、区域的定义方式区域的定义方式有XML定义的方式,以及代码定义,此处使用代码定义。新建一个WPF窗口Demo2,在Grid中,使用如下代码完成区域的定义<ContentControlGrid.Column="1"prism:RegionManager.RegionName="UserRegion"></ContentCon......
  • MVVM中ICommand的具体使用
    本节使用MVVM模式进行演示MyCommand为自定义的命令类,代码如下:publicclassMyComand:ICommand{privatereadonlyAction<object>_action;privatereadonlyFunc<object,bool>?_func;publicMyComand(Action<object>action,Func<object,bool>......
  • ICommand的实现(1)
    ICommand接口在System.Windows.Input命名空间内定义。它有两个方法和一个事件。////摘要://Occurswhenchangesoccurthataffectwhetherornotthecommandshouldexecute.eventEventHandler?CanExecuteChanged;////摘要://......
  • 腾讯云服务器Nginx反向代理的Photoprism(包括https设置)
    2024年3月15日,因为不想忘记前面关于Nginx反向代理Photoprism的内容。这里记录下,如果能帮助到点击到这篇博客的人就更好了。主要分为三个部分,第一个部分是部署Photoprism,第二个部分是Nginx反向代理,第三个部分是https设置前置条件(没有的话,就不要往下看了,或者先稍微了解下再往下看)......
  • Prism框架的用法
    今天,我向大家介绍一款WPF后台框架,以及,它的用法。官网https://prismlibrary.com/Prism框架是一个用于构建松耦合、可维护且可测试的WPF和Xamarin.Forms应用程序的开源框架。在Prism中,IContainerRegistry接口用于注册和解析应用程序中的依赖项,从而实现依赖注入和反转控......