如何:處理路由事件
此範例示範事件反昇事件運作方式,以及如何撰寫可處理路由事件資料的處理常式。
範例
在 Windows Presentation Foundation (WPF) 中,元素會排列在專案樹狀結構中。 父項目可以參與處理一開始是由項目樹狀結構中子項目所引發的事件。 這可能是事件路由所造成。
路由事件通常會遵循兩個路由策略中的其中一個:事件反昇或通道。 此範例著重于反升事件,並使用 ButtonBase.Click 事件來顯示路由的運作方式。
下列範例會建立兩 Button 個控制項,並使用 XAML 屬性語法將事件處理常式附加至通用父元素,在此範例中為 StackPanel 。 此範例會使用屬性語法將事件處理常式附加至 StackPanel 父元素,而不是附加每個 Button 子專案的個別事件處理常式。 此事件處理模式示範如何使用事件路由,作為減少已附加處理常式之項目數的技術。 每個 Button 路由透過父元素的所有反升事件。
請注意,在父 StackPanel 元素上, Click 指定為 屬性的事件名稱會透過命名 Button 類別來部分限定。 類別 Button 是 ButtonBase 衍生類別,其成員清單中具有 Click 事件。 如果所處理的事件不存在於附加路由事件處理常式之項目的成員清單中,則需要有附加事件處理常式的這個部分限定方法。
<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.RoutedEventHandle"
Name="dpanel"
Button.Click="HandleClick"
>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Height" Value="20"/>
<Setter Property="Width" Value="250"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
</StackPanel.Resources>
<Button Name="Button1">Item 1</Button>
<Button Name="Button2">Item 2</Button>
<TextBlock Name="results"/>
</StackPanel>
下列範例會處理 Click 事件。 此範例會報告哪些項目處理事件以及哪個項目引發事件。 使用者按一下任一按鈕時,就會執行事件處理常式。
public partial class RoutedEventHandle : StackPanel
{
StringBuilder eventstr = new StringBuilder();
void HandleClick(object sender, RoutedEventArgs args)
{
// Get the element that handled the event.
FrameworkElement fe = (FrameworkElement)sender;
eventstr.Append("Event handled by element named ");
eventstr.Append(fe.Name);
eventstr.Append("\n");
// Get the element that raised the event.
FrameworkElement fe2 = (FrameworkElement)args.Source;
eventstr.Append("Event originated from source element of type ");
eventstr.Append(args.Source.GetType().ToString());
eventstr.Append(" with Name ");
eventstr.Append(fe2.Name);
eventstr.Append("\n");
// Get the routing strategy.
eventstr.Append("Event used routing strategy ");
eventstr.Append(args.RoutedEvent.RoutingStrategy);
eventstr.Append("\n");
results.Text = eventstr.ToString();
}
}
Private eventstr As New Text.StringBuilder()
Private Sub HandleClick(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Get the element that handled the event.
Dim fe As FrameworkElement = DirectCast(sender, FrameworkElement)
eventstr.Append("Event handled by element named ")
eventstr.Append(fe.Name)
eventstr.Append(vbLf)
' Get the element that raised the event.
Dim fe2 As FrameworkElement = DirectCast(args.Source, FrameworkElement)
eventstr.Append("Event originated from source element of type ")
eventstr.Append(args.Source.[GetType]().ToString())
eventstr.Append(" with Name ")
eventstr.Append(fe2.Name)
eventstr.Append(vbLf)
' Get the routing strategy.
eventstr.Append("Event used routing strategy ")
eventstr.Append(args.RoutedEvent.RoutingStrategy)
eventstr.Append(vbLf)
results.Text = eventstr.ToString()
End Sub
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應