WPF中的命令简介
使用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中的命令简介的更多相关文章
- 16、WPF中的命令
一.前言 事件的作用是发布.传播一些信息,消息送达接收者,事件的使命就算完成了,至于如何响应事件送来的消息事件并不做规定,每个接收者可以使用自己的行为来响应事件,也就是说事件不具有约束力.命令能够在代 ...
- WPF中的命令与命令绑定导航
1.WPF中的命令与命令绑定(一) (引入命令) 2.WPF中的命令与命令绑定(二)(详细介绍命令和命令绑定)
- WPF中的命令与命令绑定(二)
原文:WPF中的命令与命令绑定(二) WPF中的命令与命令绑定(二) 周银辉在WPF中,命令(Commandi ...
- WPF中的命令与命令绑定(一)
原文:WPF中的命令与命令绑定(一) WPF中的命令与命令绑定(一) 周银辉说到用户输入,可能我们更多地会联想到 ...
- Windows Presentation Foundation (WPF)中的命令(Commands)简述
原文:Windows Presentation Foundation (WPF)中的命令(Commands)简述 ------------------------------------------- ...
- WPF中的图像处理简介
原文:WPF中的图像处理简介 和Winform中的GDI+相比,WPF提供了一组新的API用于显示和编辑图像.新API特点如下: 适用于新的或专用图像格式的扩展性模型. 对包括位图 (BMP).联合图 ...
- WPF中的资源简介、DynamicResource与StaticResource的区别
原文:WPF中的资源简介.DynamicResource与StaticResource的区别 什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎 ...
- WPF中的命令(Command)
这节来讲一下WPF中的命令(Command)的使用. [认识Command] 我们之前说过,WPF本身就为我们提供了一个基础的MVVM框架,本节要讲的命令就是其中一环,通过在ViewModel中声明命 ...
- WPF中的资源简介、DynamicResource与StaticResource的区别(转)
什么叫WPF的资源(Resource)?资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. 也就是 ...
随机推荐
- MongoDB学习笔记(3)--删除数据库
MongoDB 删除数据库 语法 MongoDB 删除数据库的语法格式如下: db.dropDatabase() 删除当前数据库,默认为 test,你可以使用 db 命令查看当前数据库名. 实例 以下 ...
- 10个优秀的jQuery Mobile主题
原文链接:http://caibaojian.com/10-best-free-jquery-mobile-theme.html jQuery Mobile 是一个伟大的框架,而每个伟大的产品都需要一 ...
- matlab入门笔记(七):数据文件I/O
- [转] Spring4.3.x 浅析xml配置的解析过程(6)——解析context命名空间之property-placeholder和property-override标签
在上一篇解析自定义命名空间的标签中,我们已经知道解析自定义命名空间的标签需要用到NamespaceHandler接口的实现类,并且知道spring是如何获取命名空间对应的命名空间处理器对象的.因此我们 ...
- Django基于正则表达式的URL(2)
Django基于正则表达式的URL(2) 1. 关于正则的说明 url(r'^detail-(\d+)-(\d+).html',views.detail), 当客户端输入 127.0.0.1:8000 ...
- php分享十八七:mysql基础
mysql操作数据库代码: $link = @mysql_connect('localhost:3306', 'root', 'root') or die(mysql_error()); mysql_ ...
- Word 2010之简单图文混排
所谓图文混排,就是指将图片与文本内容进行一定规律的排列,以让文档更加漂亮. 下面的示范是一个简单的将两副照片混排到文字当中的(图片与文本内容无关,仅供演示). 1. 打开Word,输入文本内容: 2. ...
- mysql获得60天前unix时间示例
在mysql中获取多少天前的unix时间的方法.首先根据now()获得当前时间,使用adddate()方法获得60天前时间,使用unix_timestamp()方法转换时间类型 select UNIX ...
- 菜鸟教程之工具使用(二)——Maven打包非规范目录结构的Web项目
用过Maven的人都知道,Maven项目的目录结构跟传统的DynamicWeb项目有些不同.当然我们按照Maven的规范建项目最好,但是当你恰好没有按照Maven的规范来,又恰好需要使用Maven来打 ...
- android笔记---百度地图api应用 (二) 获取公交路线的详细信息
package com.example.bdtest; import com.baidu.mapapi.MKEvent; import com.baidu.mapapi.MKPlanNode; imp ...