Prism DelegateCommand的详细使用

寻常使用可能就是知道其绑定的command命令

正常命名绑定

  1. <Button Content="DelegateCommand" Command="{Binding Command}" Margin="10"/>

后端

  1.   public DelegateCommand Command { get; }

构造函数中

  1.  Command = new DelegateCommand(Execute);

1使用 DelegateCommand命令绑定参数二 委托的bool表达式

参数二

是一个委托函数 bool 类型的返回值 无参函数 也能称呼为检查函数  每当在属性set中加上了 命令名称+ .RaiseCanExecuteChanged();就会再设置当前新值的时候触发当前方法CanExecute (触发指定的检查方法,方法为您自己再命令创建之初所指定的bool 返回值 无参的方法 )

当然写法 lamba表达式写法可以

2 RaiseCanExecuteChanged 触发检查函数

命令名称+ .RaiseCanExecuteChanged(); 触发检查方法 指定的前提下

同上就是当IsEnabled 为false 的时候绑定command命令的控件将不可用  为true的时候为可用

检查方法 返回的ture 或 false 会是当前command 是否能用的关键

3使用ObservesProperty 监听属性(含监听多个属性)

加上了ObservesProperty 方法并指定了一个属性或多个属性 被指定的属性我们称呼我为被监听的属性 每当监听的属性发生改变的时候那么就会触发检查函数 当然您需要有指定检查函数

示例

  1.   ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty);
  2.   //指定了检查的方法,然后再用方法指定一个监听的属性。
  3.   //每当这个属性的值 发生更改的时候我们就会去触发一次检查的方法CanExecute 
  4.   // 有了ObservesProperty当前这个方法指定监听属性之后。不需要再给属性写上。 ExecuteDelegateCommand.RaiseCanExecuteChanged();  
  5.   ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty).ObservesProperty(() => LL);
  6.   //与上不同是设置了多个监听属性                                                           

4 使用 ObservesCanExecute  可以不用指定检查方法

它与ObservesProperty 监听属性一样与之不同的是它指定监听的属性后可以不用指定检查方法 被指定的属性也不用写触发检查方法的语句

5带参数的命令 接收传递参数

  1.  ExecuteDelegateCommands = new DelegateCommand<string>(ExecuteGeneric).ObservesCanExecute(() => IsEnabled);

ExecuteGeneric 方法将只能有一个参数 想知道为什么可以看F12源码

CommandParameter 是传递的参数

Obj是 结果 也就是6

源码 前端

<Window
    x:Class="Prismcommand.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:local="clr-namespace:Prismcommand"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <StackPanel>
            <CheckBox
                Margin="10"
                Content="IsEnabled"
                IsChecked="{Binding IsEnabled}" />
            <CheckBox
                Margin="10"
                Content="MyProperty"
                IsChecked="{Binding MyProperty}" />
            <CheckBox
                Margin="10"
                Content="LL"
                IsChecked="{Binding LL}" />

            <Button
                Margin="10"
                Command="{Binding ExecuteDelegateCommand}" CommandParameter="6"
                Content="DelegateCommand" />
        

            <TextBlock
                Margin="10"
                FontSize="30"
                Text="{Binding UpdateText}" />
        </StackPanel>
    </Grid>
</Window>

后端 VIewModel

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Prismcommand
{
    public class Class1 : BindableBase
    {
        #region MyRegion
        private string _Title = "Hello";

        public string Title
        {
            get { return _Title; }
            set { SetProperty(ref _Title, value); }
        }
        private string _updateText;
        public string UpdateText
        {
            get { return _updateText; }
            set { SetProperty(ref _updateText, value); }
        } 
        #endregion
        private bool _IsEnabled;
        public bool IsEnabled
        {
            get { return _IsEnabled; }
            set{  SetProperty(ref _IsEnabled, value);}
                // ExecuteDelegateCommand.RaiseCanExecuteChanged();
                //当命令实例化没有加Observes方法时候,必须要这个,如果有添加Observes方法时候,可以去掉这个
        }
        private bool myVar;
        public bool MyProperty
        {
            get { return myVar; }
            set {   SetProperty(ref myVar, value);}
        }
        private bool ll;
        public bool LL
        {
            get { return ll; }
            set
            {
                ll = value; RaisePropertyChanged();
         //      ExecuteDelegateCommand.RaiseCanExecuteChanged();//启动command状态检查。 
            }
        }
        public DelegateCommand ExecuteDelegateCommand { get; }
        public DelegateCommand<string> ExecuteDelegateCommands { get; }
        #region MyRegion
        //public DelegateCommand DelegateCommandObservesProperty { get; }
        //public DelegateCommand DelegateCommandObservesCanExecute { get; }

        //public DelegateCommand<string> ExecuteGenericDelegateCommand { get; }

        //public ICommand command { get => new DelegateCommand(Execute).ObservesProperty(() => IsEnabled); } 
        #endregion

        public Class1()
        {
            // ExecuteDelegateCommand = new DelegateCommand(Execute);

            // ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute);
            //当前CanExecute方法是检查当前command状态是否可用。  

            //ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty);
            指定了检查的方法,然后再用方法指定一个监听的属性。
            每当这个属性的值 发生更改的时候我们就会去触发一次检查的方法CanExecute。 
             有了ObservesProperty当前这个方法指定监听属性之后。不需要再给属性写上。 ExecuteDelegateCommand.RaiseCanExecuteChanged();  
            //ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty).ObservesProperty(() => LL);
            与上不同是设置了多个监听属性                                                           
           // ExecuteDelegateCommand = new DelegateCommand(Execute).ObservesCanExecute(() => LL);
            //当前LL就是被监听的属性 被监听的属性不需要写触发检查的方法 ExecuteDelegateCommand.RaiseCanExecuteChanged()
            //也可以不写检查方法。 默认检查方法都是返回监听的这个属性 

            ExecuteDelegateCommands = new DelegateCommand<string>(ExecuteGeneric).ObservesCanExecute(() => IsEnabled);
        }

        private void ExecuteGeneric(string obj)
        {
            UpdateText = obj;
        }

        private bool CanExecute()//当然,这个方法如果返回为true,那么当前这个命令。 控件就是可以用的。reinforce当前绑定命令的控件不可用。 
        {
            return IsEnabled;
        }

        private void Execute()
        {
            UpdateText = $"Updated:{DateTime.Now.ToString()}";
        }
    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学软件开发的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值