ExecutedRoutedEventHandler 代理人

定義

代表能處理 ExecutedPreviewExecuted 路由事件,以及相關附加事件的方法。

public delegate void ExecutedRoutedEventHandler(System::Object ^ sender, ExecutedRoutedEventArgs ^ e);
public delegate void ExecutedRoutedEventHandler(object sender, ExecutedRoutedEventArgs e);
type ExecutedRoutedEventHandler = delegate of obj * ExecutedRoutedEventArgs -> unit
Public Delegate Sub ExecutedRoutedEventHandler(sender As Object, e As ExecutedRoutedEventArgs)

參數

sender
Object

附加事件處理常式的物件。

e
ExecutedRoutedEventArgs

事件資料。

範例

這個範例會建立 , ExecutedRoutedEventHandler 以在執行命令時,在目標上設定視覺屬性。 這個範例也包含 CanExecuteRoutedEventHandler 相同指令的 。

// ExecutedRoutedEventHandler for the custom color command.
private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
    Panel target = e.Source as Panel;
    if (target != null)
    {
        if (target.Background == Brushes.AliceBlue)
        {
            target.Background = Brushes.LemonChiffon;
        }
        else
        {
            target.Background = Brushes.AliceBlue;
        }
    }
}

// CanExecuteRoutedEventHandler for the custom color command.
private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (e.Source is Panel)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}
' ExecutedRoutedEventHandler for the custom color command.
Private Sub ColorCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim target As Panel = TryCast(e.Source, Panel)
    If target IsNot Nothing Then
        If target.Background Is Brushes.AliceBlue Then
            target.Background = Brushes.LemonChiffon
        Else
            target.Background = Brushes.AliceBlue
        End If

    End If
End Sub

' CanExecuteRoutedEventHandler for the custom color command.
Private Sub ColorCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    If TypeOf e.Source Is Panel Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

備註

此委派包含的 RoutedCommand實作邏輯。 將實作邏輯與 命令分開,可讓命令從不同的來源和類型叫用,並啟用命令邏輯的集中化。

此委派也用於 CommandManager.ExecutedCommandManager.PreviewExecuted,這些是實作大部分命令基礎結構之 類別上的 CommandManager 附加事件。 但最實用的處理程式會處理 Executed 來自特定 CommandBinding的事件,而不是在層級上 CommandManager 運作。

在 內 ExecutedRoutedEventArgs,當您針對從執行命令產生的路由事件撰寫事件處理程式時,通常感興趣的屬性如下:

  • Source 會報告執行命令的目標。 執行命令之後,您也可以將路由事件詞彙中的 Source 比較普遍視為引發路由事件的物件。

  • Command 會報告執行的命令。 如果您使用命令系結,而且如果您撰寫可能處理多個命令的處理程序,這個屬性會很有用。

  • Parameter 會報告執行命令所傳遞的任何命令特定參數。 並非所有命令都使用或預期命令特定的參數。

  • Handled 會報告從執行命令產生的路由事件是否已經由路由上的不同元素處理。 對於路由事件處理程式,建議的做法是讓處理程式在處理事件時執行有意義的工作,以設定 Handledtrue。 這可防止事件的一般處理程式在路由上進一步處理事件。 如需處理路由事件的詳細資訊,請參閱 將路由事件標示為已處理和類別處理

此委派也代表 和的處理程式CommandManager.Executed,這些處理程式是實作大部分命令基礎結構之 CommandManager 類別上的附加CommandManager.PreviewExecutedEvent事件。 但最實用的處理程式會處理 Executed 來自特定 CommandBinding的事件,而不是在層級上 CommandManager 運作。

如需命令的詳細資訊,請參閱命令概觀

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱