【Prism 8】命令(Command)

Prism中对ICommand接口进行了封装,使命令更容易使用。共有三种类别,分别对应三个实现类,分别为DelegateCommandDelegateCommand<T>CompositeCommand

1. DelegateCommand 与 DelegateCommand

DelegateCommandDelagateCommand<T>的使用方式基本相同,区别在于DelagateCommand<T>能够使用命令参数。在使用命令时,有几种方式可以改变命令的可执行状态

  1. 显示调用RaiseCanExecuteChanged()
  2. 创建命令时,使用ObservesProperty(Expression<Func> propertyExpression)
  3. 创建命令时,使用ObservesCanExecute(Expression<Func> canExecuteExpression)

1.1 样例

  • 创建一个名称为PrismCommandApp的WPF应用程序项目
  • 在项目根目录级别下,创建ViewsViewModels文件夹
  • MainWindow.xaml移动到Views文件夹中,并更改其名称与名称空间分别为MainWindowViewPrismCommandApp.Views
  • 打开Nuget,搜索Prism,安装Prism.Unity,此处使用的版本为8.1.97
  • 修改App.xamlApp.xaml.cs分别如下:
<unity:PrismApplication
    x:Class="PrismCommandApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:unity="http://prismlibrary.com/">
    <Application.Resources />
</unity:PrismApplication>
using System.Windows;
using Prism.Ioc;
using Prism.Modularity;
using PrismCommandApp.Views;

namespace PrismCommandApp
{
    public partial class App
    {
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
        }

        protected override Window CreateShell()
        {
            return this.Container.Resolve<MainWindowView>();
        }
    }
}
  • 由于此样例不止讲解一个知识点,因此需要对主页面进行区域划分。修改MainWindowView.xamlMainWindowView.xaml.cs分别如下:
<Window
    x:Class="PrismCommandApp.Views.MainWindowView"
    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:prism="http://prismlibrary.com/"
    xmlns:viewModels="clr-namespace:PrismCommandApp.ViewModels"
    Title="{Binding Title}"
    Width="800"
    Height="450"
    d:DataContext="{d:DesignInstance viewModels:MainWindowViewModel}"
    prism:ViewModelLocator.AutoWireViewModel="True"
    WindowStartupLocation="CenterScreen"
    mc:Ignorable="d">
    <UniformGrid Margin="5" Columns="3">
        <ContentControl prism:RegionManager.RegionName="DelegateCommandViewRegion" />
        <ContentControl prism:RegionManager.RegionName="CompositeCommandViewRegion" />
        <ContentControl prism:RegionManager.RegionName="ActiveAwareCommandViewRegion" />
    </UniformGrid>
</Window>
using Prism.Regions;

namespace PrismCommandApp.Views
{
    public partial class MainWindowView
    {
        public MainWindowView(IRegionManager regionManager)
        {
            this.InitializeComponent();

            // 使用依赖注入的方式用视图填充命名区域
            regionManager.RegisterViewWithRegion("DelegateCommandViewRegion", typeof(DelegateCommandView));
            regionManager.RegisterViewWithRegion("CompositeCommandViewRegion", typeof(CompositeCommandView));
            regionManager.RegisterViewWithRegion("ActiveAwareCommandViewRegion", typeof(ActiveAwareCommandView));
        }
    }
}
  • ViiewModels文件夹中,创建MainWindowViewModel
using Prism.Mvvm;

namespace PrismCommandApp.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string title = "Prism Command Application";

        public string Title
        {
            get => this.title;
            set => this.SetProperty(ref this.title, value);
        }
    }
}
  • Views创建名称为DelegateCommandView的WPF用户自定义控件,修改DelegateCommandView.xamlDelegateCommandView.xaml.cs分别如下:
<UserControl
    x:Class="PrismCommandApp.Views.DelegateCommandView"
    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:vm="clr-namespace:PrismCommandApp.ViewModels"
    d:DataContext="{d:DesignInstance Type=vm:DelegateCommandViewModel}"
    mc:Ignorable="d">
    <DockPanel Margin="3">
        <Border
            Background="LightGreen"
            CornerRadius="5"
            DockPanel.Dock="Top">
            <TextBlock
                Margin="0,3"
                HorizontalAlignment="Center"
                FontSize="16"
                Foreground="White">
                DelegateCommand
            </TextBlock>
        </Border>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <CheckBox
                Margin="10"
                Content="Can Execute Command"
                IsChecked="{Binding IsEnabled}" />

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

            <Button
                Margin="10"
                Command="{Binding GenericDelegateCommand}"
                CommandParameter="{Binding CommandParameter}"
                Content="DelegateCommand Generic" />

            <Button
                Margin="10"
                Command="{Binding DelegateCommandObservesProperty}"
                Content="DelegateCommand ObservesProperty" />

            <Button
                Margin="10"
                Command="{Binding DelegateCommandObservesCanExecute}"
                Content="DelegateCommand ObservesCanExecute" />

            <!--#region Task 异步执行-->
            <Button
                Margin="10"
                Command="{Binding AsyncMethodCommandOne}"
                Content="DelegateCommand AsyncMethodOne" />

            <Button
                Margin="10"
                Command="{Binding AsyncMethodCommandTwo}"
                Content="DelegateCommand AsyncMethodTwo" />
            <!--#endregion-->

            <TextBlock
                Margin="10"
                FontSize="22"
                Text="{Binding UpdateText}" />
        </StackPanel>
    </DockPanel>
</UserControl>
namespace PrismCommandApp.Views
{
    public partial class DelegateCommandView
    {
        public DelegateCommandView()
        {
            this.InitializeComponent();
        }
    }
}
  • ViewModels文件夹中,创建名称为DelegateCommandViewModel类,内容如下:
using System;
using System.Threading;
using System.Threading.Tasks;
using Prism.Commands;
using Prism.Mvvm;

namespace PrismCommandApp.ViewModels
{
    public class DelegateCommandViewModel : BindableBase
    {
        private bool isEnabled;
        public bool IsEnabled
        {
            get => this.isEnabled;
            set
            {
                this.SetProperty(ref this.isEnabled, value);
                // 命令对象主动调用RaiseCanExecuteChanged()用以告知绑定的空间,命令状态已发生改变
                this.ExecuteDelegateCommand.RaiseCanExecuteChanged();
                this.GenericDelegateCommand.RaiseCanExecuteChanged();
            }
        }

        private string updateText;
        public string UpdateText
        {
            get => this.updateText;
            set => this.SetProperty(ref this.updateText, value);
        }

        private string commandParameter = "DefaultParameter";
        public string CommandParameter
        {
            get => this.commandParameter;
            set => this.SetProperty(ref this.commandParameter, value);
        }

        public DelegateCommand ExecuteDelegateCommand { get; }
        public DelegateCommand<string> GenericDelegateCommand { get; }
        public DelegateCommand DelegateCommandObservesProperty { get; }
        public DelegateCommand DelegateCommandObservesCanExecute { get; }
        public DelegateCommand AsyncMethodCommandOne { get; }
        public DelegateCommand AsyncMethodCommandTwo { get; }

        public DelegateCommandViewModel()
        {
            // 命令的无参实现,在该实现中需要在命令可用状态发生改变时显示调用RaiseCanExecuteChanged()用以告知绑定的控件命令状态已发生改变
            // 以便控件做出正确的状态显示
            ExecuteDelegateCommand = new DelegateCommand(this.Execute, this.CanExecute);

            // 该方式与命令的无参实现几乎相同,除了该命令可带有一个object类型的参数
            GenericDelegateCommand = new DelegateCommand<string>(s => this.UpdateText = s, s => this.isEnabled);

            // 该方式是Prism提供的一种封装,不需要显示调用RaiseCanExecuteChanged()方法,而是在【被观测的属性】发生变化时,自动调用
            // 需要注意,RaiseCanExecuteChanged()方法【不会直接】对命令是否可执行做出判断,而是转嫁给了命令构造时指定的可执行判断委托
            // 因此,在使用该方式时,依然需要在命令的构造函数中指定【可执行判断委托】
            DelegateCommandObservesProperty = new DelegateCommand(this.Execute, this.CanExecute).ObservesProperty(() => this.IsEnabled);

            // 该模式时上一种模式的简化。使用于【可执行委托】的内容是一个简单的Bool属性,则可以在命令的构造函数中省略【可执行委托】
            DelegateCommandObservesCanExecute  = new DelegateCommand(this.Execute).ObservesCanExecute(() => this.IsEnabled);

            // Task异步支持
            AsyncMethodCommandOne = new DelegateCommand(this.ExecuteCommandNameOne).ObservesCanExecute(() => this.IsEnabled);
            AsyncMethodCommandTwo = new DelegateCommand(async () => await Task.Factory.StartNew(() =>
            {
                Thread.Sleep(3000);
                this.Execute();
            })).ObservesCanExecute(() => this.IsEnabled);
        }

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

        private bool CanExecute()
        {
            return this.IsEnabled;
        }

        private async void ExecuteCommandNameOne()
        {
            await Task.Factory.StartNew(() => { Thread.Sleep(3000); });
            this.Execute();
        }
    }
}
  • 运行程序,可以看到如下界面。开始时,所用按钮都处于不可执行状态,当复选框被选中后,按钮被变为可执行状态。点击按钮时,会更新信息。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

关于本小节开始时介绍的可执行状态的几种使用方式,请参加代码中的注释。

2. CompisteCommand

在使用VS等多页面可编辑的软件中,一般情况下都有一键全部保存所有改动的功能。CompisteCommand就是为了实现这种功能而设计的。
使用方式:

  • 创建一个全局可以用的CompisteCommand命令对象
  • 在每个需要关联的命令中,使用上一步创建的全局对象进行注册

2.1 样例

本节样例在上一节样例程序中继续开发。虽然是在一个项目中,但这两部分是独立的,可拆开。

这里使用依赖注入容器构建全局唯一的CompisteCommand对象。

  • 项目根目录中创建名称为ApplicationCommands的类,修改其内容如下:
using Prism.Commands;

namespace PrismCommandApp
{
    public interface IApplicationCommands
    {
        CompositeCommand SaveCommand { get; }
    }

    public class ApplicationCommands : IApplicationCommands
    {
        public CompositeCommand SaveCommand { get; } = new CompositeCommand();
    }
}
  • Views文件夹中创建名称为CompositeCommandView的WPF用户自定义控件。CompositeCommandView.xamlCompositeCommandView.xaml.cs的内容如下:
<UserControl
    x:Class="PrismCommandApp.Views.CompositeCommandView"
    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:prism="http://prismlibrary.com/"
    xmlns:vm="clr-namespace:PrismCommandApp.ViewModels"
    d:DataContext="{d:DesignInstance Type=vm:CompositeCommandViewModel}"
    mc:Ignorable="d">
    <UserControl.Resources>
        <Style TargetType="TabItem">
            <!--  ReSharper disable once Xaml.BindingWithContextNotResolved  -->
            <Setter Property="Header" Value="{Binding DataContext.Title}" />
        </Style>
    </UserControl.Resources>

    <DockPanel Margin="3">
        <Border
            Background="LightGreen"
            CornerRadius="5"
            DockPanel.Dock="Top">
            <TextBlock
                Margin="0,3"
                HorizontalAlignment="Center"
                FontSize="16"
                Foreground="White">
                CompositeCommand
            </TextBlock>
        </Border>
        <DockPanel>
            <Button
                Margin="10"
                Command="{Binding ApplicationCommands.SaveCommand}"
                Content="Save"
                DockPanel.Dock="Top" />

            <!--  指定区域名称  -->
            <TabControl Margin="10" prism:RegionManager.RegionName="ModuleForCompositeCommandViewRegion" />
        </DockPanel>
    </DockPanel>
</UserControl>
namespace PrismCommandApp.Views
{
    public partial class CompositeCommandView
    {
        public CompositeCommandView()
        {
            this.InitializeComponent();
        }
    }
}
  • ViewModels文件夹下创建名称为CompositeCommandViewModel的类,内容如下:
using Prism.Mvvm;

namespace PrismCommandApp.ViewModels
{
    public class CompositeCommandViewModel : BindableBase
    {
        private IApplicationCommands applicationCommands;

        public CompositeCommandViewModel(IApplicationCommands applicationCommands)
        {
            this.ApplicationCommands = applicationCommands;
        }

        /// <summary>
        /// 复合命令按钮绑定的命令
        /// </summary>
        public IApplicationCommands ApplicationCommands
        {
            get => this.applicationCommands;
            set => this.SetProperty(ref this.applicationCommands, value);
        }
    }
}

页面的具体内容通过Prism Model进行实现。

  • Views文件夹中,创建名称为ModuleForCompositeCommandView的WPF用户自定义控件,ModuleForCompositeCommandView.xamlModuleForCompositeCommandView.xaml.cs的内容分别如下:
<UserControl
    x:Class="PrismCommandApp.Views.ModuleForCompositeCommandView"
    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:prism="http://prismlibrary.com/"
    xmlns:vm="clr-namespace:PrismCommandApp.ViewModels"
    d:DataContext="{d:DesignInstance Type=vm:ModuleForCompositeCommandViewModel}"
    prism:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Text="{Binding Message}" />

        <TextBlock
            Margin="5"
            FontSize="18"
            Text="{Binding Title}" />

        <CheckBox
            Margin="5"
            Content="Can Execute"
            IsChecked="{Binding CanUpdate}" />

        <Button
            Margin="5"
            Command="{Binding UpdateCommand}"
            Content="Save" />

        <TextBlock Margin="5" Text="{Binding UpdateText}" />
    </StackPanel>
</UserControl>
namespace PrismCommandApp.Views
{
    public partial class ModuleForCompositeCommandView
    {
        public ModuleForCompositeCommandView()
        {
            this.InitializeComponent();
        }
    }
}
  • ViewModels文件夹中,创建名称为ModuleForCompositeCommandViewModel的类,内容如下:
using System;
using Prism.Commands;
using Prism.Mvvm;

namespace PrismCommandApp.ViewModels
{
    public class ModuleForCompositeCommandViewModel : BindableBase
    {
        private bool canUpdate = true;
        private string message = "ModuleForCompositeCommand";

        private string title;

        private string updatedText;

        public ModuleForCompositeCommandViewModel(IApplicationCommands applicationCommands)
        {
            this.UpdateCommand = new DelegateCommand(this.Update).ObservesCanExecute(() => this.CanUpdate);

            // 将本地命令注册为复合命令
            applicationCommands.SaveCommand.RegisterCommand(this.UpdateCommand);
        }

        public string Message
        {
            get => this.message;
            set => this.SetProperty(ref this.message, value);
        }

        public string Title
        {
            get => this.title;
            set => this.SetProperty(ref this.title, value);
        }

        public bool CanUpdate
        {
            get => this.canUpdate;
            set => this.SetProperty(ref this.canUpdate, value);
        }

        public string UpdateText
        {
            get => this.updatedText;
            set => this.SetProperty(ref this.updatedText, value);
        }

        public DelegateCommand UpdateCommand { get; }

        private void Update()
        {
            this.UpdateText = $"Updated: {DateTime.Now}";
        }
    }
}
  • 在项目根目录下,创建名称为ModuleForCompositeCommandModule的类,内容如下:
using System.Windows;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using PrismCommandApp.ViewModels;
using PrismCommandApp.Views;

namespace PrismCommandApp
{
    public class ModuleForCompositeCommandModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            var region = regionManager.Regions["ModuleForCompositeCommandViewRegion"];


            var tabA = containerProvider.Resolve<ModuleForCompositeCommandView>();
            SetTitle(tabA, "Tab A");
            region.Add(tabA);

            var tabB = containerProvider.Resolve<ModuleForCompositeCommandView>();
            SetTitle(tabB, "Tab B");
            region.Add(tabB);

            var tabC = containerProvider.Resolve<ModuleForCompositeCommandView>();
            SetTitle(tabC, "Tab C");
            region.Add(tabC);
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

        private static void SetTitle(FrameworkElement tab, string title)
        {
            ((ModuleForCompositeCommandViewModel) tab.DataContext).Title = title;
        }
    }
}
  • 修改App.xaml.cs的内容如下:
using System.Windows;
using Prism.Ioc;
using Prism.Modularity;
using PrismCommandApp.Views;

namespace PrismCommandApp
{
    public partial class App
    {
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<ModuleForCompositeCommandModule>();
        }

        protected override Window CreateShell()
        {
            return this.Container.Resolve<MainWindowView>();
        }
    }
}
  • 运行程序,显示如下界面。初始情况下,命令相应消息都为空,单独点击每个页面的内容时,更新各自的消息,点击顶部按钮是,更新所有也页面的消息。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

3. 带IsActive的CompoisiteCommand

有时,我们希望点击CompoisiteCommand命令按钮时,仅处于激活状态的页面被更新,非激活页面不参与功能相应。例如,在上例中,如果Tab A页面处于显示状态,Tab BTab C处于非显示状态。此时点击顶部按钮,则仅更新Tab A中的内容。
实现方式与基本的CompositeCommand有两点主要的不同:

  • 在构建全局唯一的CompositeCommand命令时,传入一个true参数
  • 实现模块的绑定数据类实现IActiveAware接口

具体实现详见源代码程序

源码

在WPF Prism MVVM中,ViewModels通常用来承载与视图相关的逻辑代码和数据。而命令则是一种将用户界面操作与ViewModel中的方法绑定起来的技术。 在ViewModel中声明命令的方式如下: ```csharp public ICommand MyCommand { get; private set; } ``` 其中,ICommand是一个接口,它定义了执行命令所需的方法。通常情况下,我们使用Prism库提供的DelegateCommand或者CompositeCommand来实现ICommand接口。 DelegateCommand表示可以通过传递Action或者Func来实现ICommand接口的类,并且它是一个泛型类型。例如: ```csharp public DelegateCommand<string> MyCommand { get; private set; } ``` CompositeCommand则是一种可以组合多个ICommand对象的类。例如: ```csharp public CompositeCommand MyCompositeCommand { get; private set; } ``` 在ViewModel的构造函数中,我们需要为命令对象赋值。例如: ```csharp public MyViewModel() { MyCommand = new DelegateCommand<string>(ExecuteMyCommand); MyCompositeCommand = new CompositeCommand(); MyCompositeCommand.RegisterCommand(MyCommand); } ``` 其中,ExecuteMyCommand是我们自定义的方法,用于执行命令的操作。 在View中,我们可以通过以下方式将命令与用户界面操作绑定起来: ```xaml <Button Content="Click Me" Command="{Binding MyCommand}" CommandParameter="Hello World"/> ``` 其中,Binding语法用于绑定ViewModel中的属性。在Command属性中,我们绑定了ViewModel中的MyCommand属性,这意味着当用户点击按钮时,MyCommand所绑定的ExecuteMyCommand方法将被调用。 而CommandParameter属性则是用来传递参数的。在这个例子中,我们将字符串"Hello World"作为参数传递给了ExecuteMyCommand方法。 总之,在WPF Prism MVVM中,ViewModels命令代码是用来将用户界面操作与ViewModel中的逻辑代码和数据绑定起来的一种技术,它可以有效地提高代码的可重用性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhy29563

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

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

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

打赏作者

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

抵扣说明:

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

余额充值