使用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集合的元素设置为绑定位置。

最新文章

  1. bzoj3316: JC loves Mkk
  2. UIButton的titleLabe setAttributeSting 首次不起作用
  3. H5项目常见问题
  4. 【目录】开源Math.NET基础数学类库使用总目录
  5. JS判断form内所有表单是否为空
  6. js兼容性问题
  7. lnmp停用nginx,改用apache
  8. 理解ThreadLocal
  9. Tasklist 命令的使用
  10. Oracle 同步表权限分配(同义词)
  11. C复习手记(Day4)
  12. 循环对XML文档添加Attribute以及移除Element 【转】
  13. ylb:SQLServer常用系统函数-字符串函数、配置函数、系统统计函数
  14. ST Lab1 junit test
  15. MySQL数据库操作常用命令
  16. layer.msg 添加在Ajax之前 显示进度条。
  17. SpringMVC工作流程描述
  18. [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组
  19. Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示
  20. React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)

热门文章

  1. U盘启动装完系统后 一拔下优盘 就不能进入系统
  2. linux的0号进程和1号进程
  3. fork()和写时复制
  4. windbg分析执行在64位环境下的32位程序的dump
  5. spring 项目中在类中注入静态字段
  6. MySql计算两个日期的时间差函数
  7. mysql中查询一个字段属于哪一个数据库中的哪一个表的方式
  8. javaweb可部署目录结构
  9. 奶瓶(beini)破解无线密码流程:安装、抓包、从虚拟机(VMware)拷贝握手包(拷贝到硬盘、U盘)、跑包
  10. c# vs2010 excel 上传oracle数据