Partilhar via


CommandManager.AddPreviewExecutedHandler Método

Definição

Anexa o ExecutedRoutedEventHandler especificado ao elemento especificado.

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)

Parâmetros

element
UIElement

O elemento ao qual anexar handler.

handler
ExecutedRoutedEventHandler

O manipulador can execute.

Exceções

element ou handler é null.

Exemplos

O exemplo a seguir cria um CanExecuteRoutedEventHandler e um ExecutedRoutedEventHandler e os anexa a um Button que é uma fonte de comando para o Help comando .

Primeiro, o Button é criado e associado ao Help comando .

<Button Command="ApplicationCommands.Help"
        Name="helpButton">Help</Button>

Em seguida, o CanExecuteRoutedEventHandler e o ExecutedRoutedEventHandler são criados.

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

Por fim, os manipuladores são anexados ao Button usando o AddCanExecuteHandler e AddExecutedHandlero .

CommandManager.AddExecutedHandler(helpButton, HelpCmdExecuted);
CommandManager.AddCanExecuteHandler(helpButton, HelpCmdCanExecute);
CommandManager.AddExecutedHandler(helpButton, AddressOf HelpCmdExecuted)
CommandManager.AddCanExecuteHandler(helpButton, AddressOf HelpCmdCanExecute)

Aplica-se a

Confira também