【Prism系列】Prism中的命令

前言

        在《【Prism系列】 Prism实现依赖注入_code bean的博客-CSDN博客》中我们讲到了prism的搭建过程,以及如何容器的使用以及自动注入的过程。

        在《【wpf】Command Binding 命令绑定的使用_code bean的博客-CSDN博客》中我们介绍了如何在前台绑定我们自定义的命令。

        今天,我们就介绍一下Prism中的命令,和我们自定义的命令其实一样,Prism也自定义了自己的命令,不过直接使用Prism提供的功能更加丰富的命令,无疑能提高我们的开发效率。

命令的使用

基础用法

prism里面定义的命令叫做DelegateCommand(引入 Prism.Commands)

前台的写法和之前的讲解的,一模一样。后台写法如下:

public DelegateCommand BtnCommand { get => new DelegateCommand(Execute); }
private void Execute()
{
    。。。。
}


//----------------------
//写法二
public DelegateCommand BtnCommand { get; set; }
void 构造函数 {
    BtnCommand = new DelegateCommand<object>(Execute1);
}

private void Execute1(object obj)
{
    。。。。。。
}

命令触发后,会关联到此方法。

带参数的命令

前台的写法和之前的讲解的,一模一样。后台写法如下:

public DelegateCommand BtnCommand { get; set; }
void 构造函数 {
    BtnCommand = new DelegateCommand<object>(Execute1);
}



private void Execute1(object obj)
{
    。。。。。。
}

这里利用泛型可以传入任意类型的参数,可谓是非常的方便。

带可用性检查的命令

public DelegateCommand BtnCommand { get; set; }
void 构造函数 {
    BtnCommand = new DelegateCommand<object>(Execute1, CheckExecute);
}


private void Execute1(object obj)
{ 
}

private bool CheckExecute(object obj)
{ 
    retrun true;
}

ObservesProperty(属性观察)

CheckExecute会重新加载时,命令触发后(Execute1执行之前),被触发。但是这样往往不够,我们希望时在某个属性变化后触发检查。写法如下:

private string test="";
public string Test
        {
            get { return test; }
            set { 
                SetProperty(ref test, value);
            }
            
        }


public DelegateCommand<object> BtnCommand2 
{ get => new DelegateCommand<object>(Execute1, CheckExecute)
    .ObservesProperty(()=>Test); }

当Test发生变化或者按键被点击都会触发这个检查(CheckExecute)

注意,这种属性观察的属性,需要这种带通知功能的。界面才会随之变换。

 ObservesCanExecute(通过bool属性检查可用性)

public bool bflag { get; set; }=false;
public DelegateCommand<object> BtnEventCommand
 { get => new DelegateCommand<object>(Execute1).ObservesCanExecute(() => bflag); }

ObservesCanExecute是结合了ObservesProperty和CheckExecute的一种写法,首先它会观察bflag是否发生变化,如果发生变换就会触发检查,是否可用以bflag的值为准。

事件转命令,任意事件的命令实现

前面文章中讲到了鼠标键盘的输入的转化位命令的方法:

 这次,介绍的是任意事件的转化为命令的方式,及其都在前台写法里面:

首先添加两个声明:

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"

这里其实就是用了Blend相关的库,只不过在装prism的时候,它自动帮你装好了。

接下来是固定写法:

比如我想将ComboBox的SelectionChanged事件触发转换成命令触发:

<ComboBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <prism:InvokeCommandAction Command="{Binding BtnEventCommand}"
                                           TriggerParameterPath=""/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ComboBoxItem>213</ComboBoxItem>
    <ComboBoxItem>345</ComboBoxItem>
    <ComboBoxItem>3000</ComboBoxItem>
</ComboBox>

 后台写法其实也是,一样的,这里要注意的是,它会自动把事件的参数传给Execute1:

public DelegateCommand<object> BtnEventCommand 
{ get => new DelegateCommand<object>(Execute1)
.ObservesCanExecute(() => bflag); }


private void Execute1(object obj)
{
    MessageBox.Show(obj?.ToString());
}

 前台中的TriggerParameterPath="",赋值为空,或者干脆不写,都是将整个事件参数进行传递,如果写了某个具体的属性,则表示仅仅传递这个属性,而不是整个事件参数。

完整测试代码奉上

<Page x:Class="WpfTest.PrismCMD"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfTest"
      xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
      xmlns:prism="http://prismlibrary.com/"
      xmlns:vm="clr-namespace:WpfTest.ViewMode"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="PrismCMD">
    <Page.DataContext>
        <vm:PrismCMDViewMode/>
    </Page.DataContext>
    <StackPanel>
        <Button Command="{Binding BtnCommand}" Name="btn1" Tag="{Binding Test}">普通命令测试</Button>
        <Button Command="{Binding BtnCommand1}" CommandParameter="{Binding Path=Content, ElementName=btn1}">带参数命令测试</Button>
        <Button Command="{Binding BtnCommand2}" CommandParameter="{Binding Path=Tag, ElementName=btn1}" >带参数命令可用测试</Button>
        <Button Command="{Binding BtnCommand3}" CommandParameter="{Binding Path=Tag, ElementName=btn1}" >带参数命令可用测试2</Button>
        <ComboBox>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <prism:InvokeCommandAction Command="{Binding BtnEventCommand}"
                                                   TriggerParameterPath=""/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <ComboBoxItem>213</ComboBoxItem>
            <ComboBoxItem>345</ComboBoxItem>
            <ComboBoxItem>3000</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Page>
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfTest.ViewMode
{
    public class PrismCMDViewMode: BindableBase
    {

        private string test="";

        public string Test
        {
            get { return test; }
            set { 
                SetProperty(ref test, value);

                if (test == "123") bflag = true;
                else bflag = false;
            }
            
        }


        public bool bflag { get; set; }=false;


        public DelegateCommand BtnCommand { get => new DelegateCommand(Execute); }
        public DelegateCommand<object> BtnCommand1 { get => new DelegateCommand<object>(Execute1); }

        //可用检查
        public DelegateCommand<object> BtnCommand2 { get => new DelegateCommand<object>(Execute1, CheckExecute).ObservesProperty(()=>Test); }
        public DelegateCommand<object> BtnCommand3 { get => new DelegateCommand<object>(Execute1).ObservesCanExecute(() => bflag); }
        public DelegateCommand<object> BtnEventCommand { get => new DelegateCommand<object>(Execute1).ObservesCanExecute(() => bflag); }
       

        private void Execute()
        {
            if (test == "")
            {
                Test = "123";
            }
            else
            {
                Test = "";
            }
            
            MessageBox.Show(test);
        }


        private void Execute1(object obj)
        {
            MessageBox.Show(obj?.ToString());
        }


        private bool CheckExecute(object obj)
        {
            //if (obj == null)
            //{ 
            //    return false;
            //}
            if (obj?.ToString() == "123") 
                return false;
            else return true;

        }
    }
}

好了,各位看官,不要吝啬您手中的赞啊~~~

posted @   宋桓公  阅读(651)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示