WPF路由事件三:自定義路由事件


與依賴項屬性類似,WPF也為路由事件提供了WPF事件系統這一組成。為一個類型添加一個路由事件的方式與為類型添加依賴項屬性的方法類似,添加一個自定義路由事件的步驟:

一、聲明路由事件變量並注冊:定義只讀的靜態變量字段RouteEvent類來聲明一個變量,然后使用EventManager的RegisterRoutedEvent()方法向事件系統注冊路由事件,該方法的簽名如下:

 1 public static RoutedEvent RegisterRoutedEvent(string name, RoutingStrategy routingStrategy, Type handlerType, Type ownerType); 

該方法帶有四個參樹:

第一個參數name表示該路由事件在WPF事件系統中的名稱。

第二個參數routingStrategy是RoutingStrategy類型的枚舉值,標明了路由事件的路由策略,共三種策略:,第一種Bubble是冒泡策略,這種模式是從觸發點向根節點傳遞,直到最外層。第二種是Direct就是傳統的事件一樣的。第三種是隧道策略,這和冒泡策略相反,向下傳遞。

第三個參數handlerType用來標明事件處理函數的類型。

第四個個參數ownerType則用來標明擁有該路由事件的類型。

EventManager的RegisterRoutedEvent()方法返回一個RoutedEvent類型的實例。一般情況下,該實例將由一個public static readonly字段所保存。

二、通過標准的.NET事件包裝路由事件:事件包裝器使用AddHandler方法來添加路由事件的調用程序,然后使用RemoveHandler來刪除已經添加的調用程序。

三、創建可以激發路由事件的方法。

演示創建自定義路由事件:

1、新建用戶控件,添加一個Button按鈕,添加按鈕的Click事件,XAML代碼如下:

 1 <UserControl x:Class="CustomWpfRouteEvent.RouteEventControl"
 2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 5              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 6              mc:Ignorable="d" 
 7              d:DesignHeight="300" d:DesignWidth="300">
 8     <Grid>
 9         <Button Height="30" Width="100" Content="調用路由事件" Click="Button_Click"></Button>
10     </Grid>
11 </UserControl>

2、在用戶控件的后台代碼中創建自定義路由事件,C#代碼如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace CustomWpfRouteEvent
17 {
18     /// <summary>
19     /// RouteEventControl.xaml 的交互邏輯
20     /// </summary>
21     public partial class RouteEventControl : UserControl
22     {
23         public RouteEventControl()
24         {
25             InitializeComponent();
26         }
27 
28         //1、聲明並注冊路由事件,使用冒泡策略
29         public static readonly RoutedEvent MyClientEvent = EventManager.RegisterRoutedEvent("MyClick",
30             RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RouteEventControl));
31 
32         //2、通過.NET事件包裝路由事件
33         public event RoutedEventHandler MyClick
34         {
35             add
36             {
37                 AddHandler(MyClientEvent, value);
38             }
39             remove
40             {
41                 RemoveHandler(MyClientEvent, value);
42             }
43         }
44 
45         /// <summary>
46         /// 3、使用按鈕的單擊事件激發路由事件
47         /// </summary>
48         /// <param name="sender"></param>
49         /// <param name="e"></param>
50         private void Button_Click(object sender, RoutedEventArgs e)
51         {
52             RoutedEventArgs arg = new RoutedEventArgs();
53             arg.RoutedEvent = MyClientEvent;
54             RaiseEvent(arg);           
55         }
56     }
57 }

3、在主界面中引入新創建的用戶控件,使用自定義的路由事件MyClick,並為MyClick事件編寫調用的方法,XAML代碼如下:

1 <Window x:Class="CustomWpfRouteEvent.MainWindow"
2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4         xmlns:u="clr-namespace:CustomWpfRouteEvent"
5         Title="演示自定義路由事件" Height="350" Width="525" WindowStartupLocation="CenterScreen">
6     <Grid>
7         <u:RouteEventControl MyClick="RouteEventControl_MyClick"></u:RouteEventControl>
8     </Grid>
9 </Window>

4、RouteEventControl_MyClick方法的后台代碼如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace CustomWpfRouteEvent
17 {
18     /// <summary>
19     /// MainWindow.xaml 的交互邏輯
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27 
28         private void RouteEventControl_MyClick(object sender, RoutedEventArgs e)
29         {
30             MessageBox.Show("Hello:" + e.Source.ToString());
31         }
32     }
33 }

5、運行程序,單擊Button按鈕,效果如下所示:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM