关于wpf的命令(Command)

轻箬笠 2021-06-30 15:13:16

《WPF深入浅出》里面关于命令的用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
namespace WpfApplication1
{
    /// <summary>
    /// Window28.xaml 的交互逻辑
    /// </summary>
    public partial class Window28 : Window
    {
        public Window28()
        {
            InitializeComponent();
            InitializeCommand();
        }
        //声明并定义命令
        private RoutedCommand rouutedCommand = new RoutedCommand("Clear",typeof(Window28));
        private void InitializeCommand()
        {
            //把命令赋值给命令源,并定义快捷键
            this.btn1.Command = rouutedCommand;
            this.rouutedCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
            //指定命令目标
            this.btn1.CommandTarget = txtA;
 
            //创建命令关联
            CommandBinding commandBinding = new CommandBinding();
            commandBinding.Command = rouutedCommand;//只关注与rouutedCommand相关的命令
            commandBinding.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);
            commandBinding.Executed += new ExecutedRoutedEventHandler(cb_Execute);
            //把命令关联安置在外围控件上
            this.sp1.CommandBindings.Add(commandBinding);
        }
        //当命令到达目标之后,此方法被调用
        private void cb_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            txtA.Clear();
            //避免事件继续向上传递而降低程序性能
            e.Handled = true;
        }
        //当探测命令是否可执行的时候该方法会被调用
        private void cb_CanExecute(object sender,CanExecuteRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtA.Text))
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
            //避免事件继续向上传递而降低程序性能
            e.Handled = true;
        }
 
    }
}

引自WPF学习之深入浅出话命令

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Input;

/***********************作者:黄昏前黎明后**********************************

*   作者:黄昏前黎明后

*   CLR版本:4.0.30319.42000

*   创建时间:2018-04-05 22:57:56

*   命名空间:Example3

*   唯一标识:b9043d4c-fdd7-4e0f-a324-00f0f09286d0

*   机器名称:HLPC

*   联系人邮箱:hl@cn-bi.com

*

*   描述说明:

*

*   修改历史:

*

*

*****************************************************************/

namespace Example3

{

    public class RelayCommand : ICommand

    {

        #region 字段

        readonly Func<Boolean> _canExecute;

        readonly Action _execute;

        #endregion

        #region 构造函数

        public RelayCommand(Action execute)

            : this(execute, null)

        {

        }

        public RelayCommand(Action execute, Func<Boolean> canExecute)

        {

            if (execute == null)

                throw new ArgumentNullException("execute");

            _execute = execute;

            _canExecute = canExecute;

        }

        #endregion

        #region ICommand的成员

        public event EventHandler CanExecuteChanged

        {

            add

            {

                if (_canExecute != null)

                    CommandManager.RequerySuggested += value;

            }

            remove

            {

                if (_canExecute != null)

                    CommandManager.RequerySuggested -= value;

            }

        }

        [DebuggerStepThrough]

        public Boolean CanExecute(Object parameter)

        {

            return _canExecute == null ? true : _canExecute();

        }

        public void Execute(Object parameter)

        {

            _execute();

        }

        #endregion

    }

}

引自WPF自学入门(十一)WPF MVVM模式Command命令

 

为什么感觉mvvm里面的Command比《WPF深入浅出》里面提到的简单很多呢?这里面进行了封装?或者两者之间的区别是什么?

 

...全文
1203 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhishiheng 2021-07-05
  • 打赏
  • 举报
回复

一样的兄弟,mvvm 进行了封装

轻箬笠 2021-07-05
  • 打赏
  • 举报
回复
RoutedCommand routedCommand = command as RoutedCommand;
                if (routedCommand != null)
                {
                    if (routedCommand.CriticalCanExecute(parameter,
                                                    target,
                                                    inputEventArgs.UserInitiated /*trusted*/,
                                                    out continueRouting))
                    {
                        // If the command can be executed, we never continue to route the
                        // input event.
                        continueRouting = false;
 
                        ExecuteCommand(routedCommand, parameter, target, inputEventArgs);
                    }
                }
                else
                {
                    if (command.CanExecute(parameter))
                    {
                        command.Execute(parameter);
                    }
                }

wpf社区的人好少。看了下源码,上面这里产生了分差,相当于有两种方式处理Command。
.net源码

Lvwl-CN 2021-07-02
  • 打赏
  • 举报
回复

了解一下接口ICommand,无论如何实现,都是一个原理

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧