如何:啟用命令
下列範例示範如何在 Windows Presentation Foundation (WPF) 中使用命令。 此範例示範如何將 關聯 RoutedCommand 至 、建立 CommandBinding ,以及建立實作 的 RoutedCommandButton 事件處理常式。 如需命令的詳細資訊,請參閱 命令概觀 。
範例
程式碼的第一個區段會建立使用者介面 (UI),其中包含 Button 和 StackPanel ,並建立 CommandBinding 將命令處理常式與 RoutedCommand 建立關聯的 。
Command的 Button 屬性與 Close 命令相關聯。
會 CommandBinding 加入根 CommandBindingCollectionWindow 的 。 Executed和 CanExecute 事件處理常式會附加至這個系結,並與 命令相關聯 Close 。
CommandBinding如果沒有 命令邏輯,則只有叫用命令的機制。 Button按一下 時, PreviewExecutedRoutedEvent 會在命令目標上引發 ,後面接著 ExecutedRoutedEvent 。 這些事件會周遊元素樹狀結構,尋找 CommandBinding 該特定命令的 。 值得注意的是,因為 RoutedEvent 通道和泡泡通過元素樹狀結構,所以必須小心放置 的位置 CommandBinding 。 CommandBinding如果 位於命令目標的同層級或不在 路由 RoutedEvent 上的另一個節點上, CommandBinding 將不會存取 。
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CloseCommand"
Name="RootWindow"
>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"
CanExecute="CanExecuteHandler"
/>
</Window.CommandBindings>
<StackPanel Name="MainStackPanel">
<Button Command="ApplicationCommands.Close"
Content="Close File" />
</StackPanel>
</Window>
// Create ui elements.
StackPanel CloseCmdStackPanel = new StackPanel();
Button CloseCmdButton = new Button();
CloseCmdStackPanel.Children.Add(CloseCmdButton);
// Set Button's properties.
CloseCmdButton.Content = "Close File";
CloseCmdButton.Command = ApplicationCommands.Close;
// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);
// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);
' Create ui elements.
Dim CloseCmdStackPanel As New StackPanel()
Dim CloseCmdButton As New Button()
CloseCmdStackPanel.Children.Add(CloseCmdButton)
' Set Button's properties.
CloseCmdButton.Content = "Close File"
CloseCmdButton.Command = ApplicationCommands.Close
' Create the CommandBinding.
Dim CloseCommandBinding As New CommandBinding(ApplicationCommands.Close, AddressOf CloseCommandHandler, AddressOf CanExecuteHandler)
' Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding)
程式碼的下一節會 Executed 實作 和 CanExecute 事件處理常式。
處理常式 Executed 會呼叫 方法來關閉開啟的檔案。 處理常式 CanExecute 會呼叫 方法來判斷檔案是否開啟。 如果檔案已開啟, CanExecute 則會設定為 true
,否則會設定為 false
。
// Executed event handler.
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
// Calls a method to close the file and release resources.
CloseFile();
}
// CanExecute event handler.
private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
// Call a method to determine if there is a file open.
// If there is a file open, then set CanExecute to true.
if (IsFileOpened())
{
e.CanExecute = true;
}
// if there is not a file open, then set CanExecute to false.
else
{
e.CanExecute = false;
}
}
' Executed event handler.
Private Sub CloseCommandHandler(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
' Calls a method to close the file and release resources.
CloseFile()
End Sub
' CanExecute event handler.
Private Sub CanExecuteHandler(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
' Call a method to determine if there is a file open.
' If there is a file open, then set CanExecute to true.
If IsFileOpened() Then
e.CanExecute = True
' if there is not a file open, then set CanExecute to false.
Else
e.CanExecute = False
End If
End Sub
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應