CommandManager.AddExecutedHandler 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的 ExecutedRoutedEventHandler 附加到指定的元素。
public:
static void AddExecutedHandler(System::Windows::UIElement ^ element, System::Windows::Input::ExecutedRoutedEventHandler ^ handler);
public static void AddExecutedHandler (System.Windows.UIElement element, System.Windows.Input.ExecutedRoutedEventHandler handler);
static member AddExecutedHandler : System.Windows.UIElement * System.Windows.Input.ExecutedRoutedEventHandler -> unit
Public Shared Sub AddExecutedHandler (element As UIElement, handler As ExecutedRoutedEventHandler)
参数
- element
- UIElement
要向其附加 handler
的元素。
- handler
- ExecutedRoutedEventHandler
执行的处理程序。
例外
element
或 handler
为 null
。
示例
以下示例创建 CanExecuteRoutedEventHandler 和 , ExecutedRoutedEventHandler 并将其附加到 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)