zoukankan      html  css  js  c++  java
  • WPF MVVM

    原文:http://www.cnblogs.com/yapzhang/archive/2010/08/27/1810226.html

    代码:RelayCommand  RouteCommand/RouteUICommand/DelegateCommand 

    基础知识

    1、命令模式

    命令(Command)模式属于对象的行为模式【GOF95】。命令模式又称为行动(Action)模式或交易(Transaction)模式。命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

    命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。

    每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。

    2、Aciton<T>委托封装一个方法,该方法只有一个参数并且不返回值。(Encapsulates a method that has a single parameter and does not return a value.)

    3、How to implement a reusable ICommand

    If you are using the MVVM (model-view-viewmodel) pattern, one of the most used mechanism to bind actions to the view are commands. To provide a command, you have to implement the ICommand interface. This is quite simple, but you have to do it over and over again. This is cumbersome.

    The idea of this pattern build an universal command, that takes two delegates: One that is called, whenICommand.Execute(object param) is invoked, and one that evalues the state of the command whenICommand.CanExecute(object param) is called.

    In addition to this, we need a method, that triggers the CanExecuteChanged event. This causes the ui element to reevaluate the CanExecute() of the commmand.

    public class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;
     
        public event EventHandler CanExecuteChanged;
     
        public DelegateCommand(Action<object> execute) 
                       : this(execute, null)
        {
        }
     
        public DelegateCommand(Action<object> execute, 
                       Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }
     
        public override bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }
     
            return _canExecute(parameter);
        }
     
        public override void Execute(object parameter)
        {
            _execute(parameter);
        }
     
        public void RaiseCanExecuteChanged()
        {
            if( CanExecuteChanged != null )
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
    SampleCode

    4、Difference between Delegatecommand, relaycommand and routedcommand

    I'm confused about command pattern. There are so many different explanations about the commands. I thought the code below was delegatecommand, but after reading about the relaycommand, I am in doubt.

    What is the difference between relaycommand, delegatecommand and routedcommand. Is it possible to show in examples that have relevance to my posted code?

    class FindProductCommand : ICommand
    {
        ProductViewModel _avm;
    
        public FindProductCommand(ProductViewModel avm)
        {
            _avm = avm;
        }
    
        public bool CanExecute(object parameter)
        {
            return _avm.CanFindProduct();
        }
    
        public void Execute(object parameter)
        {
            _avm.FindProduct();
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
    }
    问题代码

    ANSWER:

    Your class is a FindProductCommand which implements the interface ICommand so it can be used as a WPF command. It is neither a DelegateCommand nor a RelayCommand, nor is it aRoutedCommand, which are other implementations of the ICommand interface.

    Generally, when an implementation of ICommand is named DelegateCommand or RelayCommand, the intention is that you should not have to implement the ICommand interface, but rather pass the necessary methods as parameters to the RelayCommand constructor. Instead of your entire class, you could write:

    ProductViewModel _avm;varFindPoductCommand=newDelegateCommand<object>((parameter)=> _avm.FindProduct(),(parameter)=> _avm.CanFindProduct());

    As far as the difference between RoutedCommand and RelayCommand/DelegateCommand, see here.

  • 相关阅读:
    数据挖掘竞赛常用代码段
    东南大学《算法设计基础》课程作业 4
    东南大学《算法设计基础》课程作业 3
    东南大学《算法设计基础》课程作业 2
    东南大学《算法设计基础》课程作业 1
    shiro
    你好2021!
    关系型数据库索引设计与优化
    lua table面向对象扩展
    lua、python对比学习
  • 原文地址:https://www.cnblogs.com/gmth/p/3446731.html
Copyright © 2011-2022 走看看