使用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. Easyui 中 Tabsr的常用方法

    注:index必须为变量 tab页从0开始 //新增一个tab页var index = 0;$('#tt').tabs('add',{ title: 'Tab'+index, content: '&l ...

  2. Could not connect to Redis at xxx.xxx.xxx.xxx:6379: Connection refused

    开发发来消息说测试环境的redis无法登录: # redis-cli -p 6379 -h xxx.xxx.xxx.xxx Could not connect to Redis at xxx.xxx. ...

  3. Linux 的僵尸(zombie)进程

    可能很少有人意识到,在一个进程调用了exit之后,该进程 并非马上就消失掉,而是留下一个称为僵尸进程(Zombie)的数据结构.在Linux进程的5种状态中,僵尸进程是非常特殊的一种,它已经放弃了几乎 ...

  4. (面试题)两个对象值相同 (x.equals(y) == true) ,但却可有不同的 hash code ,这 句话对不对

    答:不对,有相同的 hash code这是java语言的定义:1) 对象相等则hashCode一定相等:2) hashCode相等对象未必相等 1.如果是基本变量,没有hashcode和equals方 ...

  5. 启动和停止kafka 及kafka manager

    启动kafka: sh /app/pet_kafka_xxxx_cluster/bin/kafka-server-start.sh -daemon /app/pet_kafka_xxxx_cluste ...

  6. jota-time 练习

    public static void main(String[] args) { LocalDate now = new LocalDate(); //输出形式为yyyy-MM-dd System.o ...

  7. dubbo调用服务出现如下异常

    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA ...

  8. hive splict, explode, lateral view, concat_ws

    hive> create table arrays (x array<string>) > row format delimited fields terminated by ...

  9. Centos 7 使用iptables

    systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 #安装 ...

  10. android辅助开发工具包介绍

    辅助开发工具包(ADK)是为硬件制造商和业余爱好者准备的参考实现.硬件制造商和业余爱好者可以使用此工具包作为开发Android辅助设备的起点.每一个ADK发行版都将提供源代码和硬件规格,以使整个辅助设 ...