CommandManager.AddPreviewExecutedHandler 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的 ExecutedRoutedEventHandler 附加至指定的項目。
public:
static void AddPreviewExecutedHandler(System::Windows::UIElement ^ element, System::Windows::Input::ExecutedRoutedEventHandler ^ handler);
public static void AddPreviewExecutedHandler (System.Windows.UIElement element, System.Windows.Input.ExecutedRoutedEventHandler handler);
static member AddPreviewExecutedHandler : System.Windows.UIElement * System.Windows.Input.ExecutedRoutedEventHandler -> unit
Public Shared Sub AddPreviewExecutedHandler (element As UIElement, handler As ExecutedRoutedEventHandler)
參數
- element
- UIElement
handler
要附加的目標項目。
- handler
- ExecutedRoutedEventHandler
為可以執行處理常式。
例外狀況
element
或 handler
為 null
。
範例
下列範例會 CanExecuteRoutedEventHandler 建立 和 ExecutedRoutedEventHandler ,並將其附加至 Button ,這是命令的 Help 命令來源。
首先,會 Button 建立 並與 命令相關聯 Help 。
<Button Command="ApplicationCommands.Help"
Name="helpButton">Help</Button>
接下來,會建立 CanExecuteRoutedEventHandler 和 ExecutedRoutedEventHandler。
private void HelpCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
// OpenHelpFile opens the help file
OpenHelpFile();
}
Private Sub HelpCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
' OpenHelpFile opens the help file
OpenHelpFile()
End Sub
private void HelpCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
// HelpFilesExists() determines if the help file exists
if (HelpFileExists() == true)
{
e.CanExecute = true;
}
else
{
e.CanExecute = false;
}
}
Private Sub HelpCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
' HelpFilesExists() determines if the help file exists
If HelpFileExists() = True Then
e.CanExecute = True
Else
e.CanExecute = False
End If
End Sub
最後,處理程式會使用 AddCanExecuteHandler 和AddExecutedHandler附加至 Button 。
CommandManager.AddExecutedHandler(helpButton, HelpCmdExecuted);
CommandManager.AddCanExecuteHandler(helpButton, HelpCmdCanExecute);
CommandManager.AddExecutedHandler(helpButton, AddressOf HelpCmdExecuted)
CommandManager.AddCanExecuteHandler(helpButton, AddressOf HelpCmdCanExecute)