65.9K
CodeProject 正在变化。 阅读更多。
Home

WPF 命令:基础知识

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (3投票s)

2009年5月14日

CPOL

2分钟阅读

viewsIcon

23458

初次接触 WPF 中的命令。

介绍 

WPF 中的命令与 WinForms 中的传统事件不同。命令最初设计用于在应用程序级别使用,但它们已成为开发者在 UI 编程中最受欢迎的功能之一(我们倾向于比应该使用的更多)。命令允许开发者定义一次任务,然后将其“附加”多次,而无需通过传统方式,这需要复制代码或在多个地方调用它。(这就是它的魔力。)WPF 本身提供了五个开箱即用的命令。

  1. ApplicationCommands(应用程序命令)
  2. ComponentCommands(组件命令)
  3. EditingCommands(编辑命令)
  4. MediaCommands(媒体命令)
  5. NavigationCommands(导航命令)

我探索过它们,并且喜欢上了 ApplicationCommands。(事实上,大多数开发者都会使用它。)

要使用命令,请执行以下操作:

  1. 将自定义/预定义命令链接到您希望响应该命令的控件,并为该命令添加输入手势。
  2. 为命令创建一个处理程序,并使用 CommandBindings 类将处理程序绑定到控件。
  3. 将绑定添加到控件的 Commands 集合。

所以让我们深入研究一些代码来演示 WPF 命令。

首先,我们来看一下 XAML 代码。

<Window x:Class="Commanding.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="340" Width="509" 
xmlns:MyCommands="clr-namespace:Commanding"> 
<Grid> 
<Border Padding="5" BorderBrush="Black" 
		BorderThickness="2" CornerRadius="5" Margin="10"> 
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" 
				VerticalAlignment="Top" Height="64"> 
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" 
				VerticalAlignment="Center"> 
<Label>Type Your Name :</Label> 
<TextBox Name="txtName" Width="200"></TextBox> 
<Button Command="MyCommands:Commands.HelloCommand" 
				Content="Say Hello" Padding="3"></Button> 
</StackPanel> 
<CheckBox Name="chkCanExecute" 
		Content="Uncheck to suppress command execution" IsChecked="True"/> 
</StackPanel> 
</Border> 
</Grid> 
</Window> 

XAML 代码中的关键部分是名为 MyCommands 的命名空间和 Button 元素。该命名空间…

xmlns:MyCommands="clr-namespace:Commanding 

…引用了定义自定义命令的命名空间。

Button 元素的 Command 属性与自定义命令关联。

<Button Command="MyCommands: CustomCommand.HelloCommand" 
			Content="Say Hello" Padding="3"></Button> 

下面您将看到 CustomCommand 类中的 HelloCommand 属性。

namespace Commanding 
{ 
public class CustomCommand 
{ 
private static System.Windows.Input.RoutedUICommand helloCommand; 
static CustomCommand () 
{ 
// First: I created a gesture collections 
System.Windows.Input.InputGestureCollection gestureCollection 
= new System.Windows.Input.InputGestureCollection(); 
//Second: I add the input (Key gesture) that I want to trigger this command) 
gestureCollection.Add(new System.Windows.Input.KeyGesture
	(System.Windows.Input.Key.H, System.Windows.Input.ModifierKeys.Control)); 
//Third: Initialize my command 
helloCommand = new System.Windows.Input.RoutedUICommand
	("HelloCommand", "HelloCommand", typeof(Commands), gestureCollection); 
} 
public static System.Windows.Input.RoutedUICommand HelloCommand 
{ 
get { return helloCommand; } 
} 
} 
}

下面我绑定自定义命令中的属性,然后分配一个处理程序并将其添加到 Window 的 CommandBindings。请注意,我没有直接将其绑定到按钮控件。

namespace Commanding 
{ 
/// <summary> 
/// Interaction logic for Window1.xaml 
/// </summary> 
public partial class Window1 : Window 
{ 
public Window1() 
{ 
InitializeComponent(); 
CommandBinding binding = new CommandBinding(); 
binding.Command = Commanding.Commands.HelloCommand; 
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed); 
binding.CanExecute += new CanExecuteRoutedEventHandler(binding_CanExecute); 
this.CommandBindings.Add(binding);//Commenting out this line disables the button 
} 
void binding_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
e.CanExecute = (bool)chkCanExecute.IsChecked; 
} 
void binding_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
MessageBox.Show(string.Format("Hello {0}, you have mastered commanding", txtName.Text)); 
} 
} 
} 

注意 binding_CanExecute 方法。这提供了条件执行。当您订阅此事件时,首先调用此方法以查看是否可以调用您的命令。我们使用复选框来启用或禁用它。

在下一篇文章中,我们将探讨命令和 MVVM(模型-视图-ViewModel)。在这里,您将看到命令的力量。Window1 类中的代码违反了许多 GUI 原则。使用 MVVM,您将拥有一个集中的位置(ViewModel),您可以在其中拥有可重用的命令。

有关更简单的代码,请访问 www.olivercode.net

历史

  • 2009 年 5 月 14 日:初始发布
© . All rights reserved.