这一节我们来演示一下ApplicationCommands预定义命令的使用,通过一个建议的记事本开发流程,掌握ApplicationCommands预定义命令的用法。
使用预定义命令,最重要的三个地方,第一点,如何通过CommandBinding对象去关联一个Command,第二点,如何编写命令的业务代码,第三点,命令如何触发,或者说,命令如何绑定到命令源对象。
第一点,通过CommandBinding对象去关联一个Command
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Open"
CanExecute="OpenCommandCanExecute"
Executed="OpenCommandExecuted" />
<CommandBinding Command="ApplicationCommands.Cut"
CanExecute="CutCommandCanExecute"
Executed="CutCommandExecuted" />
<CommandBinding Command="ApplicationCommands.Paste"
CanExecute="PasteCommandCanExecute"
Executed="PasteCommandExecuted" />
<CommandBinding Command="ApplicationCommands.Save"
CanExecute="SaveCommandCanExecute"
Executed="SaveCommandExecuted" />
</Window.CommandBindings>
在Window窗体的CommandBindings集合中,我们实例化了4个CommandBinding对象,分别关联了Open、Cut、Paste和Save,对应打开、剪切、粘贴和保存。
第二点,如何编写命令的业务代码
private void OpenCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void OpenCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "文本文档 (.txt)|*.txt",
Multiselect = true
};
var result = openFileDialog.ShowDialog();
if (result.Value)
{
textbox.Text = File.ReadAllText(openFileDialog.FileName);
}
}
private void CutCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = textbox != null && textbox.SelectionLength > 0;
}
private void CutCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
textbox.Cut();
}
private void PasteCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Clipboard.ContainsText();
}
private void PasteCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
textbox.Paste();
}
private void SaveCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = textbox != null && textbox.Text.Length > 0;
}
private void SaveCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
var saveFileDialog = new Microsoft.Win32.SaveFileDialog
{
Filter = "文本文档 (.txt)|*.txt"
};
if (saveFileDialog.ShowDialog() == true)
{
File.WriteAllText(saveFileDialog.FileName , textbox.Text);
}
}
我们需要给Command的CanExecute和Executed成员分别创建各自的回调函数,并在函数中编写自己的业务逻辑。
第三点,命令如何绑定到命令源对象
绑定命令的方式通常是通过控件的事件、或者响应键盘或鼠标的操作去实现的,在这里,我们演示这两种方法,第一种是通过快捷键去绑定上面的命令。
<Window.InputBindings>
<KeyBinding Key="O" Modifiers="Control" Command="ApplicationCommands.Open" />
<KeyBinding Key="X" Modifiers="Control" Command="ApplicationCommands.Cut" />
<KeyBinding Key="V" Modifiers="Control" Command="ApplicationCommands.Paste" />
<KeyBinding Key="S" Modifiers="Control" Command="ApplicationCommands.Save" />
</Window.InputBindings>
这些KeyBinding可以定义快捷键,并指向某个命令。第二种方式就是创建一个前端控件,比如实例化一个按钮,利用按钮的Command绑定命令。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="打开" Margin="5" Command="ApplicationCommands.Open"/>
<Button Content="剪切" Margin="5" Command="ApplicationCommands.Cut"/>
<Button Content="粘贴" Margin="5" Command="ApplicationCommands.Paste"/>
<Button Content="保存" Margin="5" Command="ApplicationCommands.Save"/>
</StackPanel>
<TextBox x:Name="textbox" Grid.Row="1" Margin="5" TextWrapping="Wrap">
</TextBox>
</Grid>
坦白说,WPF的预定义命令并不常用,但是作为开发者,还是很有必要全面了解一下相关知识。我们更多时候还是使用ICommand的自定义命令,下一讲,我们将介绍控件的其它事件如何去引用一个命令。
当前课程源码下载:(注明:本站所有源代码请按标题搜索)
文件名:071-《ApplicationCommands与记事本开发实战》-源代码
链接:https://pan.baidu.com/s/1yu-q4tUtl0poLVgmcMfgBA
提取码:wpff
——重庆教主 2023年10月12日
WPF 提供一组预定义命令。命令库包括以下类:ApplicationCommands、NavigationCommands、MediaCommands、EditingCommands 和 ComponentCommands。 这些类提供诸如 Cut、BrowseBack、BrowseForward、Play、Stop 和 Pause 的命令。这些类仅由 RoutedCommand 对象构成,而不包含命令的实现逻辑。 实现逻辑由在其上执行命令的对象负责。
我们来看看这几个类的定义。
一、ApplicationCommands的定义
public static class ApplicationCommands
{
public static RoutedUICommand Cut { get; }
public static RoutedUICommand Stop { get; }
public static RoutedUICommand ContextMenu { get; }
public static RoutedUICommand Properties { get; }
public static RoutedUICommand PrintPreview { get; }
public static RoutedUICommand CancelPrint { get; }
public static RoutedUICommand Print { get; }
public static RoutedUICommand SaveAs { get; }
public static RoutedUICommand Save { get; }
public static RoutedUICommand Close { get; }
public static RoutedUICommand CorrectionList { get; }
public static RoutedUICommand Open { get; }
public static RoutedUICommand Help { get; }
public static RoutedUICommand SelectAll { get; }
public static RoutedUICommand Replace { get; }
public static RoutedUICommand Find { get; }
public static RoutedUICommand Redo { get; }
public static RoutedUICommand Undo { get; }
public static RoutedUICommand Delete { get; }
public static RoutedUICommand Paste { get; }
public static RoutedUICommand Copy { get; }
public static RoutedUICommand New { get; }
public static RoutedUICommand NotACommand { get; }
}
二、NavigationCommands 的定义
public static class NavigationCommands
{
public static RoutedUICommand BrowseBack { get; }
public static RoutedUICommand BrowseForward { get; }
public static RoutedUICommand BrowseHome { get; }
public static RoutedUICommand BrowseStop { get; }
public static RoutedUICommand Refresh { get; }
public static RoutedUICommand Favorites { get; }
public static RoutedUICommand Search { get; }
public static RoutedUICommand IncreaseZoom { get; }
public static RoutedUICommand DecreaseZoom { get; }
public static RoutedUICommand Zoom { get; }
public static RoutedUICommand NextPage { get; }
public static RoutedUICommand PreviousPage { get; }
public static RoutedUICommand FirstPage { get; }
public static RoutedUICommand LastPage { get; }
public static RoutedUICommand GoToPage { get; }
public static RoutedUICommand NavigateJournal { get; }
}
三、MediaCommands的定义
public static class MediaCommands
{
public static RoutedUICommand Play { get; }
public static RoutedUICommand DecreaseMicrophoneVolume { get; }
public static RoutedUICommand IncreaseMicrophoneVolume { get; }
public static RoutedUICommand BoostBass { get; }
public static RoutedUICommand DecreaseBass { get; }
public static RoutedUICommand IncreaseBass { get; }
public static RoutedUICommand DecreaseTreble { get; }
public static RoutedUICommand IncreaseTreble { get; }
public static RoutedUICommand MuteVolume { get; }
public static RoutedUICommand DecreaseVolume { get; }
public static RoutedUICommand IncreaseVolume { get; }
public static RoutedUICommand Select { get; }
public static RoutedUICommand TogglePlayPause { get; }
public static RoutedUICommand ChannelDown { get; }
public static RoutedUICommand ChannelUp { get; }
public static RoutedUICommand Rewind { get; }
public static RoutedUICommand FastForward { get; }
public static RoutedUICommand PreviousTrack { get; }
public static RoutedUICommand NextTrack { get; }
public static RoutedUICommand Record { get; }
public static RoutedUICommand Stop { get; }
public static RoutedUICommand Pause { get; }
public static RoutedUICommand MuteMicrophoneVolume { get; }
public static RoutedUICommand ToggleMicrophoneOnOff { get; }
}
四、EditingCommands的定义
public static class EditingCommands
{
public static RoutedUICommand ToggleInsert { get; }
public static RoutedUICommand SelectDownByParagraph { get; }
public static RoutedUICommand SelectUpByParagraph { get; }
public static RoutedUICommand SelectDownByPage { get; }
public static RoutedUICommand SelectUpByPage { get; }
public static RoutedUICommand SelectToLineStart { get; }
public static RoutedUICommand SelectToLineEnd { get; }
public static RoutedUICommand SelectToDocumentStart { get; }
public static RoutedUICommand SelectToDocumentEnd { get; }
public static RoutedUICommand ToggleBold { get; }
public static RoutedUICommand ToggleItalic { get; }
public static RoutedUICommand ToggleUnderline { get; }
public static RoutedUICommand ToggleSubscript { get; }
public static RoutedUICommand ToggleSuperscript { get; }
public static RoutedUICommand IncreaseFontSize { get; }
public static RoutedUICommand DecreaseFontSize { get; }
public static RoutedUICommand AlignLeft { get; }
public static RoutedUICommand AlignCenter { get; }
public static RoutedUICommand AlignRight { get; }
public static RoutedUICommand AlignJustify { get; }
public static RoutedUICommand ToggleBullets { get; }
public static RoutedUICommand ToggleNumbering { get; }
public static RoutedUICommand IncreaseIndentation { get; }
public static RoutedUICommand DecreaseIndentation { get; }
public static RoutedUICommand SelectUpByLine { get; }
public static RoutedUICommand SelectDownByLine { get; }
public static RoutedUICommand SelectLeftByWord { get; }
public static RoutedUICommand SelectRightByWord { get; }
public static RoutedUICommand Delete { get; }
public static RoutedUICommand Backspace { get; }
public static RoutedUICommand DeleteNextWord { get; }
public static RoutedUICommand DeletePreviousWord { get; }
public static RoutedUICommand EnterParagraphBreak { get; }
public static RoutedUICommand EnterLineBreak { get; }
public static RoutedUICommand TabForward { get; }
public static RoutedUICommand TabBackward { get; }
public static RoutedUICommand MoveRightByCharacter { get; }
public static RoutedUICommand MoveLeftByCharacter { get; }
public static RoutedUICommand MoveRightByWord { get; }
public static RoutedUICommand CorrectSpellingError { get; }
public static RoutedUICommand MoveLeftByWord { get; }
public static RoutedUICommand MoveUpByLine { get; }
public static RoutedUICommand MoveDownByParagraph { get; }
public static RoutedUICommand MoveUpByParagraph { get; }
public static RoutedUICommand MoveDownByPage { get; }
public static RoutedUICommand MoveUpByPage { get; }
public static RoutedUICommand MoveToLineStart { get; }
public static RoutedUICommand MoveToLineEnd { get; }
public static RoutedUICommand MoveToDocumentStart { get; }
public static RoutedUICommand MoveToDocumentEnd { get; }
public static RoutedUICommand SelectRightByCharacter { get; }
public static RoutedUICommand SelectLeftByCharacter { get; }
public static RoutedUICommand MoveDownByLine { get; }
public static RoutedUICommand IgnoreSpellingError { get; }
}
五、ComponentCommands的定义
public static class ComponentCommands
{
public static RoutedUICommand ScrollPageUp { get; }
public static RoutedUICommand MoveFocusBack { get; }
public static RoutedUICommand MoveFocusForward { get; }
public static RoutedUICommand MoveFocusDown { get; }
public static RoutedUICommand MoveFocusUp { get; }
public static RoutedUICommand SelectToPageDown { get; }
public static RoutedUICommand SelectToPageUp { get; }
public static RoutedUICommand SelectToEnd { get; }
public static RoutedUICommand SelectToHome { get; }
public static RoutedUICommand ExtendSelectionRight { get; }
public static RoutedUICommand ExtendSelectionLeft { get; }
public static RoutedUICommand ExtendSelectionDown { get; }
public static RoutedUICommand MoveFocusPageUp { get; }
public static RoutedUICommand ExtendSelectionUp { get; }
public static RoutedUICommand MoveToPageUp { get; }
public static RoutedUICommand MoveToEnd { get; }
public static RoutedUICommand MoveToHome { get; }
public static RoutedUICommand MoveDown { get; }
public static RoutedUICommand MoveUp { get; }
public static RoutedUICommand MoveRight { get; }
public static RoutedUICommand MoveLeft { get; }
public static RoutedUICommand ScrollByLine { get; }
public static RoutedUICommand ScrollPageRight { get; }
public static RoutedUICommand ScrollPageLeft { get; }
public static RoutedUICommand ScrollPageDown { get; }
public static RoutedUICommand MoveToPageDown { get; }
public static RoutedUICommand MoveFocusPageDown { get; }
}
这些命令都是RoutedUICommand类型,RoutedUICommand的父类是RoutedCommand,而RoutedCommand继承了ICommand接口。所以,本质上,RoutedUICommand也是一个ICommand对象。我们需要了解一下它的父类的定义
public class RoutedCommand : ICommand
{
public RoutedCommand();
public RoutedCommand(string name, Type ownerType);
public RoutedCommand(string name, Type ownerType, InputGestureCollection inputGestures);
public string Name { get; }
public Type OwnerType { get; }
public InputGestureCollection InputGestures { get; }
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter, IInputElement target);
public void Execute(object parameter, IInputElement target);
}
这些预先定义好的命令并没有实现业务逻辑,如果要实现业务逻辑,需要用到CommandBinding对象。下一节,我们将介绍CommandBinding对象在命令与逻辑代码中间发挥了什么关键性的作用。
——重庆教主 2023年10月11日