中继命令的完整实现-可以应用于所有情况吗? [英] Full implementation of Relay Command - can it be applied to all cases?

查看:84
本文介绍了中继命令的完整实现-可以应用于所有情况吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看中继命令的完整实现,可以在此处

I'm looking at the full implementation of a Relay Command that can be found here

我听说RelayCommand背后的想法是要有一种"通用遥控器 "以用于所有命令.

I heard that the idea behind RelayCommand is to have a sort of "universal remote control" to use on all your commands.

如果是这种情况,我在实施过程中会遇到2个问题:

If that is the case, I have 2 issues with the implementation:

1)如果对于某些控件我不想传递参数会怎样?我一定要吗?我是否需要相应地更改执行/可以执行功能以支持这些参数?

1) What happens if for certain controls I don't want to pass a parameter? Do I have to? Do I need to change my execute/can-execute functions accordingly to support these parameters?

2)如果我不想在XAML中传递CommandParameter怎么办?如果我想通过更改属性或代码中的其他方法来影响控件的更改.是否可以在不通过XAML传递CommandParameter的情况下实现CanExecute或CanExecuteChanged?

2) What if I don't want to pass a CommandParameter in XAML? If I want to affect the change of the control by using Property changed, or some other method in my code. Can I effect CanExecute or CanExecuteChanged without passing a CommandParameter in the XAML?

直到现在,我主要实现了部分RelayCommands,其中CanExecute始终返回true,并且我只是将控件IsEnabled绑定到了视图模型中的一个额外属性.这个效果很好,但是我想知道-完整的实现可以为我做些什么?

Until now I mainly implemented partial RelayCommands where CanExecute returned true all the time, and I simply binded controls IsEnabled to an extra property in my viewmodel. This works pretty good, but I wonder - what can the full implementation do for me?

(答案能否给出一个完整的工作示例?)

( Can the answer give a full working example please? )

推荐答案

1)ICommand仅具有包含参数的方法.如果未在XAML中指定参数,则使用null.

1) ICommand only has methods that include a parameter. If you don't specify a parameter in XAML, null is used.

https://msdn.microsoft.com/zh-CN/library/system.windows.input.icommand(v=vs.110).aspx

2)是的,您可以在没有CommandParameter的情况下实现CanExecute.如下所示,它在CanExecute中使用视图模型的字符串属性"MyData".

2) Yes, you can effect CanExecute without a CommandParameter. See below, it uses the viewmodel's string property "MyData" in CanExecute.

MainWindow.xaml

MainWindow.xaml

<Window x:Class="WpfApplication8.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication8"
        mc:Ignorable="d"
        FocusManager.FocusedElement="{Binding ElementName=tb}"
        SizeToContent="Height"
        Title="MainWindow" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <StackPanel>
        <Label Content="MyData (CanExecute returns false if this is whitespace)" />
        <TextBox Name="tb" Text="{Binding MyData, UpdateSourceTrigger=PropertyChanged}" Margin="5" />
        <Button Content="Without XAML CommandParameter" Margin="5" Command="{Binding Command1}" />
        <Button Content="With XAML CommandParameter" Margin="5" Command="{Binding Command1}" CommandParameter="{Binding MyData}" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

MainWindow.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication8
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private ICommand command1;
        public ICommand Command1 { get { return command1; } set { command1 = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Command1))); } }

        private string myData;
        public string MyData { get { return myData; } set { myData = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyData))); } }

        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindowViewModel()
        {
            Command1 = new RelayCommand<object>(Command1Execute, Command1CanExecute);
        }

        private bool Command1CanExecute(object obj)
        {
            // Only allow execute if MyData has data
            return !string.IsNullOrWhiteSpace(MyData);
        }

        private void Command1Execute(object obj)
        {
            MessageBox.Show($"CommandParameter = '{obj}'");
        }
    }

    public class RelayCommand<T> : ICommand
    {
        #region Fields

        readonly Action<T> _execute = null;
        readonly Predicate<T> _canExecute = null;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
        /// </summary>
        /// <param name="execute">Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.</param>
        /// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand Members

        ///<summary>
        ///Defines the method that determines whether the command can execute in its current state.
        ///</summary>
        ///<param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        ///<returns>
        ///true if this command can be executed; otherwise, false.
        ///</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }

        ///<summary>
        ///Occurs when changes occur that affect whether or not the command should execute.
        ///</summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        ///<summary>
        ///Defines the method to be called when the command is invoked.
        ///</summary>
        ///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null" />.</param>
        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }

        #endregion
    }
}

这篇关于中继命令的完整实现-可以应用于所有情况吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆