使用Prism委托命令Demo: WPF委托命令DelegateCommand的传参方式

在WPF中使用命令的步骤很简单

1.创建命令

2.绑定命令

3.设置命令源

4.设置命令目标

WPF中命令的核心是System.Windows.Input.ICommand接口,所有命令对象都实现了此接口。当创建自己的命令时,不能直接实现ICommand接口,而是要使用System.Windows.Input.RouteCommand类,该类已经实现了ICommand接口,所有WPF命令都是RouteCommand类的实例。在程序中处理的大部分命令不是RoutedCommand对象,而是RoutedUICommand类的实例,它继承自RouteCommand类。

WPF提供了一个命令库,命令库中提供了多个常用的命令,命令库通过5个专门的静态类的静态属性来提供。

5个静态类分别为:

ApplicationCommands 该类提供通用命令,包括Copy、Cut、Paste等等。

NavigationCommands 该类提供了导航的命令。

EditingCommands 该类提供了很多主要的文档编辑命令 如MoveToLineEnd、MoveLeftByWord等等。

CommponentCommands 该类提供了用户界面元素使用的命令。

MediaCommands 该类提供了一组用于处理多媒体的命令。

MSDN帮助文档搜索ApplicaiontCommands、NavigationCommands等可以看到命令详细用法

通过上面5个静态类的静态属性可以获得常用的命令对象。

下面的XAML示例演示了将命令源关联到按钮,代码如下。

<Window x:Class="WPF命令详解.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="ApplicationCommands.Open"></Button>
<Button Grid.Row="1" Command="ApplicationCommands.New"></Button>
<Button Grid.Row="2" Command="ApplicationCommands.Save"></Button>
<Button Grid.Row="3" Command="ApplicationCommands.Copy"></Button>
</Grid>
</Window>

从上面的代码中可以看到,通过Command关联命令对象,当应用程序执行时,会发现按钮都是不可用的,变成了不可用状态与IsEnable属性设置为False一样。这是因为按钮还没有关联绑定,下面看一下关键绑定后的代码如下。

XAML代码如下。

<Window x:Class="WPF命令详解.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
Executed="CommandBinding_Executed">
</CommandBinding>
</Window.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="ApplicationCommands.Open"></Button>
<Button Grid.Row="1" Command="ApplicationCommands.New"></Button>
<Button Grid.Row="2" Command="ApplicationCommands.Save"></Button>
<Button Grid.Row="3" Command="ApplicationCommands.Copy"></Button>
</Grid>
</Window>

CS代码如下。

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.Navigation;
using System.Windows.Shapes; namespace WPF命令详解
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
CommandBinding binding = new CommandBinding(ApplicationCommands.New);
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
CommandBinding cmd_Open = new CommandBinding(ApplicationCommands.Open);
cmd_Open.Executed += new ExecutedRoutedEventHandler(cmd_Open_Executed);
CommandBinding cmd_Save = new CommandBinding(ApplicationCommands.Save);
cmd_Save.Executed += new ExecutedRoutedEventHandler(cmd_Save_Executed); this.CommandBindings.Add(binding);
this.CommandBindings.Add(cmd_Open);
this.CommandBindings.Add(cmd_Save);
} void cmd_Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("保存");
} void cmd_Open_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("打开");
} void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("新建");
} private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("复制");
}
}
}

从上面的代码中可以看到,在XAML代码中可以实现命令绑定。

 <Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
Executed="CommandBinding_Executed">
</CommandBinding>
</Window.CommandBindings>

也可以在CS代码中实现命令绑定。

 CommandBinding binding = new CommandBinding(ApplicationCommands.New);
binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);
this.CommandBindings.Add(binding);

还有就是要写Executed事件中的代码。

 void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("新建");
}

上面的内容是通过实现了ICommandSource接口的Button控件来触发执行的命令,下面演示了直接调用命令的方式,代码如下。

ApplicationCommands.Open.Execute(null, this);
CommandBindings[].Command.Execute(null);

第一种方法使用了命令对象的Execute方法调用命令,此方法接收两个参数,第一个参数是传递的参数值,第二个参数是命令绑定的所在位置,示例中使用了当前窗体。

第二种方法在关联的CommandBinding对象中调用Execute方法,对于这种情况不需要提供命令绑定的所在位置,因为会自动将提供正在使用的CommandBindings集合的元素设置为绑定位置。

WPF中的命令简介的更多相关文章

  1. 16、WPF中的命令

    一.前言 事件的作用是发布.传播一些信息,消息送达接收者,事件的使命就算完成了,至于如何响应事件送来的消息事件并不做规定,每个接收者可以使用自己的行为来响应事件,也就是说事件不具有约束力.命令能够在代 ...

  2. WPF中的命令与命令绑定导航

    1.WPF中的命令与命令绑定(一) (引入命令) 2.WPF中的命令与命令绑定(二)(详细介绍命令和命令绑定)

  3. WPF中的命令与命令绑定(二)

    原文:WPF中的命令与命令绑定(二) WPF中的命令与命令绑定(二)                                              周银辉在WPF中,命令(Commandi ...

  4. WPF中的命令与命令绑定(一)

    原文:WPF中的命令与命令绑定(一)   WPF中的命令与命令绑定(一)                                           周银辉说到用户输入,可能我们更多地会联想到 ...

  5. Windows Presentation Foundation (WPF)中的命令(Commands)简述

    原文:Windows Presentation Foundation (WPF)中的命令(Commands)简述 ------------------------------------------- ...

  6. WPF中的图像处理简介

    原文:WPF中的图像处理简介 和Winform中的GDI+相比,WPF提供了一组新的API用于显示和编辑图像.新API特点如下: 适用于新的或专用图像格式的扩展性模型. 对包括位图 (BMP).联合图 ...

  7. WPF中的资源简介、DynamicResource与StaticResource的区别

    原文:WPF中的资源简介.DynamicResource与StaticResource的区别 什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎 ...

  8. WPF中的命令(Command)

    这节来讲一下WPF中的命令(Command)的使用. [认识Command] 我们之前说过,WPF本身就为我们提供了一个基础的MVVM框架,本节要讲的命令就是其中一环,通过在ViewModel中声明命 ...

  9. WPF中的资源简介、DynamicResource与StaticResource的区别(转)

    什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. 也就是 ...

随机推荐

  1. 跟我学SharePoint 2013视频培训课程——排序、过滤在列表、库中的使用(10)

    课程简介 第10天,SharePoint 2013排序.过滤在列表.库中的使用. 视频 SharePoint 2013 交流群 41032413

  2. 最优化方法:共轭梯度法(Conjugate Gradient)

    http://blog.csdn.net/pipisorry/article/details/39891197 共轭梯度法(Conjugate Gradient) 共轭梯度法(英语:Conjugate ...

  3. hashCode方法

    hashCode方法: 当覆写(override)了equals()方法之后,必须也覆写hashCode()方法,反之亦然.这个方法返回一个整型值(hash code value),如果两个对象被eq ...

  4. Java数据结构和算法(十):二叉树

    一.简介 二叉树是树这种数据结构的一员,后面我们还会介绍红黑树,2-3-4树等数据结构.那么为什么要使用树?它有什么优点? 前面我们介绍数组的数据结构,我们知道对于有序数组,查找很快,并介绍可以通过二 ...

  5. 处理 ASP.NET 中的异常:无法在发送 HTTP 标头之后进行重定向。

    因为在 Global.asax 中的 Application_Error 事件中添加了统一的错误处理,其中会有 Redirect 重定向到错误页面. 但是有可能有些情况下已经进行过其它重定向操作,所以 ...

  6. mongodb导出数据表命令之mongoexport

    mongoexport导出文件格式支持csv和json,不同的是csv格式必须显示的指定要导出的字段,如: mongoexport -d rbac -c rbacs -o d:/web/rbac.cs ...

  7. Debugging the Java HotSpot VM

    Open Heart Surgery: Analyzing and Debugging the Java HotSpot VM at the OS Level https://www.youtube. ...

  8. (原创)C++11改进我们的程序之右值引用

    本次主要讲c++11中的右值引用,后面还会讲到右值引用如何结合std::move优化我们的程序. c++11增加了一个新的类型,称作右值引用(R-value reference),标记为T & ...

  9. bash(3):遍历文件

    #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] then g ...

  10. 记录-UEFI启动的预装WIN8的笔记本里引导linux双系统

    新买了个联想笔记本,预装了WIN8,引导方式不再是几年前的MBR-BOIS引导了,是UEFI引导,所以,之前的grub4dos引导双系统方式都没用了. 现在把我装linux的关键过程记录下来,以备忘. ...