標籤:space exec pac component command 隧道 editing change 大小
命令簡介
WPF 中的命令是通過實現 ICommand 介面建立的。ICommand 公開兩個方法(Execute 及 CanExecute)和一個事件(CanExecuteChanged)。Execute 執行與命令關聯的操作。CanExecute 確定是否可以在當前命令目標上執行命令。如果集中管理命令操作的命令管理器檢測到命令源中發生了更改,此更改可能使得已引發但尚未由命令綁定執行的命令無效,則將引發 CanExecuteChanged。ICommand 的 WPF 實現是 RoutedCommand 類。
WPF 中的主要輸入源是滑鼠、鍵盤、墨跡和路由命令。更加面向裝置的輸入使用 RoutedEvent 來通知應用程式頁中的對象已發生了輸入事件。RoutedCommand 沒有不同。RoutedCommand 的 Execute 和 CanExecute 方法不包含命令的應用程式邏輯,而是引發這樣的路由事件:沿元素樹以隧道和冒泡形式傳遞,直到遇到具有 CommandBinding 的對象。CommandBinding 包含這些事件的處理常式,執行此命令的就是這些處理常式。
RoutedCommand 上的 Execute 方法在命令目標上引發 PreviewExecuted 和 Executed 事件。RoutedCommand 上的 CanExecute 方法在命令目標上引發 CanExecute 和 PreviewCanExecute 事件。這些事件沿元素樹以隧道和冒泡形式傳遞,直到遇到具有該特定命令的 CommandBinding 的對象。
WPF 提供了一組常用的路由命令,這組命令分布在幾個類中:MediaCommands、ApplicationCommands、NavigationCommands、ComponentCommands 和 EditingCommands。這些類僅包含 RoutedCommand 對象,而不包含命令的實現邏輯。實現邏輯由其上執行命令的對象負責。[1]
自訂命令
除了上述WPF 內建的RoutedCommand,還可以使用RoutedUICommand 類建立使用者自訂命令,下面將通過一個執行個體詳細講解。首先建立一個WPF 項目,在其中加入一個TextBlock。目的是通過快速鍵組合“Ctrl+Alt+I”和“Ctrl+Alt+D”改變字型大小,由“Ctrl+Alt+C”隨機改變字型顏色。
<Grid> <TextBlock x:Name="textBlock1" Text="Hello World" HorizontalAlignment="Center" FontSize="10" Margin="42,29,46,41" Width="293" /> </Grid>
首先在Window.Resources 中定義兩個RoutedUICommand,分別用於增加和減小字型尺寸。
<Window.Resources> <RoutedUICommand x:Key="IncreaseFontSize" Text="Increase Font Size" /> <RoutedUICommand x:Key="DecreaseFontSize" Text="Decrease Font Size" /></Window.Resources>
通過KeyBinding 為上面兩個命令綁定快速鍵,按鍵組合可使用“+”進行串連。下面代碼分別通過Modifiers+Key 和Gesture 兩種方式為定義快速鍵組合方式。大家可以任選其一進行使用,MSDN 中建議使用Gesture 方式定義以免發生混淆。
<Window.InputBindings> <KeyBinding Modifiers="Ctrl+Alt" Key="I" Command="{StaticResource IncreaseFontSize}"/> <KeyBinding Gesture="Ctrl+Alt+D" Command="{StaticResource DecreaseFontSize}"/></Window.InputBindings>
接下來就要通過CanExecute和Excuted 為命令綁定相關的事件,CanExecute 負責判斷能否執行命令(即Executed 定義的事件),Executed 就負責去執行使用者定義的操作命令。
<Window.CommandBindings> <CommandBinding Command="{StaticResource IncreaseFontSize}" CanExecute="CommandBinding_Increase_CanExecute" Executed="CommandBinding_Increase_Executed"/> <CommandBinding Command="{StaticResource DecreaseFontSize}" CanExecute="CommandBinding_Decrease_CanExecute" Executed="CommandBinding_Decrease_Executed"/></Window.CommandBindings>
至此,我們在XAML 中對命令的定義已經完成。下面進入到C# 中編寫命令事件相關內容。擴大字型尺寸時通過CommandBinding_Increase_CanExecute 判斷當前字型是否小於50,否則不會執行Executed 命令。若字型尺寸在50以內則通過CommandBinding_Increase_Executed 每次增加5。縮小尺寸時則不低於5。
private void CommandBinding_Increase_CanExecute(object sender, CanExecuteRoutedEventArgs e){ if (textBlock1.FontSize > 50) { e.CanExecute = false; } else { e.CanExecute = true; }}private void CommandBinding_Increase_Executed(object sender, ExecutedRoutedEventArgs e){ textBlock1.FontSize += 5;}private void CommandBinding_Decrease_CanExecute(object sender, CanExecuteRoutedEventArgs e){ if (textBlock1.FontSize <= 5) { e.CanExecute = false; } else { e.CanExecute = true; }}private void CommandBinding_Decrease_Executed(object sender, ExecutedRoutedEventArgs e){ textBlock1.FontSize -= 5;}
運行程式使用“Ctrl+Alt+I”或 “Ctrl+Alt+D”改變字型大小。
除了在XAML 中定義RoutedUICommand 我們也可以直接用C#定義,下面繼續完成修改字型顏色的快速鍵命令。建立一個CustomCommand 類,在其中加入如下代碼定義ChangeFontColor 命令。
using System.Windows.Input;namespace WpfUserControlTest{ class CustomCommand { public static readonly RoutedUICommand ChangeFontColor = new RoutedUICommand("Change Font Color", "ChangeFontColor", typeof(MainWindow)); }}
在MainWindow.xaml <Window> 中加入命名空間,以便後面調用ChangeFontColor 命令。
xmlns:c="clr-namespace:WpfUserControlTest"
在<Window.InputBindings>中為ChangeFontColor 添加快速鍵組合。
<KeyBinding Modifiers="Control+Alt" Key="C" Command="c:CustomCommand.ChangeFontColor"/>
在<Window.CommandBindings>中添加CanExecute、Excuted 命令事件。
<CommandBinding Command="c:CustomCommand.ChangeFontColor" CanExecute="CommandBinding_Color_CanExecute" Executed="CommandBinding_Color_Executed"/>
當使用者點擊“Ctrl+Alt+C”是觸發命令事件,最近改變字型顏色。
private void CommandBinding_Color_CanExecute(object sender, CanExecuteRoutedEventArgs e){ e.CanExecute = true;}private void CommandBinding_Color_Executed(object sender, ExecutedRoutedEventArgs e){ Random rd = new Random(); textBlock1.Foreground = new SolidColorBrush( Color.FromRgb( (byte)rd.Next(0,255), (byte)rd.Next(0, 255), (byte)rd.Next(0, 255)) );}
WPF 自訂快速鍵命令(COMMAND)(轉)