MVVM中的DelegateCommand (转)

Command2种玩法,分别是DelegateCommandCompositeCommand,它们都派生于ICommand接口。

ICommand的接口类型定义如下:

    public interface ICommand

    {

        event EventHandler CanExecuteChanged;

        bool CanExecute(object parameter);

        void Execute(object parameter);

}
  1. Command上不是有2个方法吗?Execute和CanExecute。首先执行CanExecute,根据返回值决定是否要执行Execute。但我们通常不进行CanExecute判断,而直接执行Execute,也就是说,使用这个DelegateCommand<T>泛型类的第一个构造函数:
        public DelegateCommand<object> ClickCommand = new DelegateCommand<object>(OnClick);

 

另外,根据C# 3.0中的lambda表达式语法,也可以改写为如下形式:

 

 public DelegateCommand<object> ClickCommand = new DelegateCommand<object>(OnClick, arg => true);

 

  1. 注意,Button实现了ICommandSource接口,其中Command属性是只读的,既然用不到它的set方法,那么就让我们把它设置为private set,如下所示:
public DelegateCommand<object> ClickCommand { get; private set; }

 

这也从侧面说明了CanExecute和Execute两个方法只能在Command的构造函数中初始化。

 

  1. 出于惰性声明的思想,我们将Command声明为ICommand类型,而在构造函数中将其实例化为具体的类型,于是大家常常会看到这样的语句:

 

            public DelegateCommand<object> ClickCommand { get; private set; }

 

//以下实例化语句出现在其它方法中,也就是需要实例化的时候

ClickCommand = new DelegateCommand<object>(OnClick, e => true);

例子:

 

Delegate Command Example in MVVM

Introduction

The Delegate Command can be used by implementing the System.Windows.Input.ICommand interface, the following bellow code for use of delegate command :

XAML Code

 

<Window x:Class="CommandMVVM.View.CharacterView"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

mc:Ignorable="d"

d:DesignHeight="332" d:DesignWidth="477">

  <Grid>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width="Auto" />

      <ColumnDefinition Width="*" />

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

      <RowDefinition Height="Auto" />

    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0" Content="Comic character name:" Margin="5" />

    <Label Grid.Column="0" Grid.Row="1" Content="Secret identity:" Margin="5" />

    <Label Grid.Column="0" Grid.Row="2" Content="Good guy?" Margin="5" />

    <Label Grid.Column="0" Grid.Row="3" Content="Arch-enemy" Margin="5" />

    <Label Grid.Column="0" Grid.Row="4" Content="Powers" Margin="5" />

    <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Model.Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Model.SecretIdentity, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

    <CheckBox Grid.Column="1" Grid.Row="2" IsChecked="{Binding Model.Hero, UpdateSourceTrigger=PropertyChanged}" Margin="5,12,5,10"/>

    <ListBox Grid.Column="1" Grid.Row="3" Margin="5"

    ItemsSource="{Binding Model.Powers}">

      <ListBox.ItemTemplate>

        <DataTemplate>

          <Label Content="{Binding Description}" />

        </DataTemplate>

      </ListBox.ItemTemplate>

    </ListBox>

    <TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Model.ArchEnemy.Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" />

    <!--<Button Grid.Column="1" Grid.Row="5" Content="Save"

Width="65" Height="25" HorizontalAlignment="Right"

Command="{Binding SaveCommand}"/>-->

    <Button Grid.Column="1" Grid.Row="5" Content="{Binding SaveCommand2.Label}"

    Width="65" Height="25" HorizontalAlignment="Right"

    Command="{Binding SaveCommand2}"/>

  </Grid>

  </Window >

Add a new class in the ViewModes folder having following content

Code Behind

 

using System;

using System.Windows;

using CommandMVVM.Model;

using CommandMVVM.Framework;

 

namespace CommandMVVM.ViewModel

{

    public class ComicCharacterViewModel : ObjectBase

    {

        public ComicCharacterViewModel()

            : this(Character.Create())

        {

        }

        public ComicCharacterViewModel(Character model)

        {

            Model = model;

            SaveCommand = new SaveCharacterCommand(this);

            SaveCommand2 = new DelegateCommand<object>(SaveCommand_Execute, SaveCommand_CanExecute);

        }

        public Character Model { get; private set; }

 

        public SaveCharacterCommand SaveCommand { get; private set; }

 

        public DelegateCommand<object> SaveCommand2 { get; private set; }

 

        void SaveCommand_Execute(object arg)

        {

            // obviously this should not be done in the viewmodel

            MessageBox.Show(string.Format("{0} character saved.", Model.Name));

        }

        bool SaveCommand_CanExecute(object arg)

        {

            return Model.Name != string.Empty;

        }

    }

}

For binding the ComicCharacterViewModel class to windows class write the following code in to  the Initialized event 

ViewModel.ComicCharacterViewModel CCVM = new ViewModel.ComicCharacterViewModel();
            this.DataContext = CCVM;
  链接:http://www.cnblogs.com/dudu/articles/1577056.html

posted @ 2013-12-12 15:12  邹邹  Views(3065)  Comments(0)    收藏  举报