C# Prism 框架使用(五) 命令

Prism(五) 命令

源码地址 - WineMonk/PrismStudy: Prism框架学习 (github.com)

委托命令

MainWindow.xaml

<Window x:Class="UsingDelegateCommands.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="Using DelegateCommand" Width="350" Height="275">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <CheckBox IsChecked="{Binding IsEnabled}" Content="Can Execute Command" Margin="10"/>
        <Button Command="{Binding ExecuteDelegateCommand}" Content="DelegateCommand" Margin="10"/>
        <Button Command="{Binding DelegateCommandObservesProperty}" Content="DelegateCommand ObservesProperty" Margin="10"/>
        <Button Command="{Binding DelegateCommandObservesCanExecute}" Content="DelegateCommand ObservesCanExecute" Margin="10"/>
        <Button Command="{Binding ExecuteGenericDelegateCommand}" CommandParameter="Passed Parameter" Content="DelegateCommand Generic" Margin="10"/>
        <TextBlock Text="{Binding UpdateText}" Margin="10" FontSize="22"/>
    </StackPanel>
</Window>

MainWindowViewModel.cs

public class MainWindowViewModel : BindableBase
{
    private bool _isEnabled;
    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            SetProperty(ref _isEnabled, value);
            ExecuteDelegateCommand.RaiseCanExecuteChanged();
        }
    }

    private string _updateText;
    public string UpdateText
    {
        get { return _updateText; }
        set { SetProperty(ref _updateText, value); }
    }


    public DelegateCommand ExecuteDelegateCommand { get; private set; }

    public DelegateCommand<string> ExecuteGenericDelegateCommand { get; private set; }        

    public DelegateCommand DelegateCommandObservesProperty { get; private set; }

    public DelegateCommand DelegateCommandObservesCanExecute { get; private set; }


    public MainWindowViewModel()
    {
        ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute);

        DelegateCommandObservesProperty = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => IsEnabled);

        DelegateCommandObservesCanExecute = new DelegateCommand(Execute).ObservesCanExecute(() => IsEnabled);

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

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

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

    private bool CanExecute()
    {
        return IsEnabled;
    }
}

复合命令

bandicam 2024-03-04 15-01-16-089

TabView模块

TabView.xaml

<UserControl x:Class="ModuleA.Views.TabView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"             
             prism:ViewModelLocator.AutoWireViewModel="True">
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
        <TextBlock Text="{Binding Title}" FontSize="18" Margin="5"/>
        <CheckBox IsChecked="{Binding CanUpdate}" Content="Can Execute" Margin="5"/>
        <Button Command="{Binding UpdateCommand}" Content="Save" Margin="5"/>
        <TextBlock Text="{Binding UpdateText}"  Margin="5"/>
    </StackPanel>
</UserControl>

TabViewViewModel.cs

public class TabViewModel : BindableBase
{
    IApplicationCommands _applicationCommands;

    private string _title;
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    private bool _canUpdate = true;
    public bool CanUpdate
    {
        get { return _canUpdate; }
        set { SetProperty(ref _canUpdate, value); }
    }

    private string _updatedText;
    public string UpdateText
    {
        get { return _updatedText; }
        set { SetProperty(ref _updatedText, value); }
    }

    public DelegateCommand UpdateCommand { get; private set; }

    public TabViewModel(IApplicationCommands applicationCommands)
    {
        _applicationCommands = applicationCommands;

        UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);

        _applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
    }

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

公共模块

IApplicationCommands.cs

public interface IApplicationCommands
{
    CompositeCommand MyCompositeCommand { get; }
}

public class ApplicationCommands : IApplicationCommands
{
    private CompositeCommand _saveCommand = new CompositeCommand();
    public CompositeCommand MyCompositeCommand
    {
        get { return _saveCommand; }
    }
}

主程序

MainWindow.xaml

<Window x:Class="UsingCompositeCommands.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">

    <Window.Resources>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding DataContext.Title}" />
        </Style>
    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button Content="Save" Margin="10" Command="{Binding ApplicationCommands.SaveCommand}"/>

        <TabControl Grid.Row="1" Margin="10" prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>

App.xaml.cs

public partial class App : PrismApplication
{
    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

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

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();
    }
}

复合命令扩展:指向当前活动视图的命令

bandicam 2024-03-04 15-27-52-203

TabViewModel.cs

// public class TabViewModel : BindableBase
public class TabViewModel : BindableBase, IActiveAware
{
    IApplicationCommands _applicationCommands;

    private string _title;
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    private bool _canUpdate = true;
    public bool CanUpdate
    {
        get { return _canUpdate; }
        set { SetProperty(ref _canUpdate, value); }
    }

    private string _updatedText;
    public string UpdateText
    {
        get { return _updatedText; }
        set { SetProperty(ref _updatedText, value); }
    }

    public DelegateCommand UpdateCommand { get; private set; }

    public TabViewModel(IApplicationCommands applicationCommands)
    {
        _applicationCommands = applicationCommands;

        UpdateCommand = new DelegateCommand(Update).ObservesCanExecute(() => CanUpdate);

        _applicationCommands.SaveCommand.RegisterCommand(UpdateCommand);
    }

    private void Update()
    {
        UpdateText = $"Updated: {DateTime.Now}";
    }
	
    // 添加代码 ↓↓↓
    bool _isActive;
    public bool IsActive
    {
        get { return _isActive; }
        set
        {
            _isActive = value;
            OnIsActiveChanged();
        }
    }
    private void OnIsActiveChanged()
    {
        UpdateCommand.IsActive = IsActive;

        IsActiveChanged?.Invoke(this, new EventArgs());
    }
    public event EventHandler IsActiveChanged;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Winemonk

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

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

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

打赏作者

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

抵扣说明:

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

余额充值