WPF自定義路由事件
與依賴(lài)項(xiàng)屬性類(lèi)似,WPF也為路由事件提供了WPF事件系統(tǒng)這一組成。為一個(gè)類(lèi)型添加一個(gè)路由事件的方式與為類(lèi)型添加依賴(lài)項(xiàng)屬性的方法類(lèi)似,添加一個(gè)自定義路由事件的步驟:
一、聲明路由事件變量并注冊(cè)
定義只讀的靜態(tài)變量字段RouteEvent類(lèi)來(lái)聲明一個(gè)變量,然后使用EventManager的RegisterRoutedEvent()方法向事件系統(tǒng)注冊(cè)路由事件,該方法的簽名如下:
public?static?RoutedEvent RegisterRoutedEvent(string?name, RoutingStrategy routingStrategy, Type handlerType, Type ownerType);?
該方法帶有四個(gè)參數(shù):
- 第一個(gè)參數(shù)name表示該路由事件在WPF事件系統(tǒng)中的名稱(chēng)。
- 第二個(gè)參數(shù)routingStrategy是RoutingStrategy類(lèi)型的枚舉值,標(biāo)明了路由事件的路由策略,共三種策略:
- 第一種Bubble是冒泡策略,這種模式是從觸發(fā)點(diǎn)向根節(jié)點(diǎn)傳遞,直到最外層。
- 第二種是Direct就是傳統(tǒng)的事件一樣的。
- 第三種是隧道策略,這和冒泡策略相反,向下傳遞。
- 第三個(gè)參數(shù)handlerType用來(lái)標(biāo)明事件處理函數(shù)的類(lèi)型。
- 第四個(gè)個(gè)參數(shù)ownerType則用來(lái)標(biāo)明擁有該路由事件的類(lèi)型。
EventManager的RegisterRoutedEvent()方法返回一個(gè)RoutedEvent類(lèi)型的實(shí)例。一般情況下,該實(shí)例將由一個(gè)public static readonly字段所保存。
二、通過(guò)標(biāo)準(zhǔn)的.NET事件包裝路由事件
事件包裝器使用AddHandler方法來(lái)添加路由事件的調(diào)用程序,然后使用RemoveHandler來(lái)刪除已經(jīng)添加的調(diào)用程序。
三、創(chuàng)建可以激發(fā)路由事件的方法
演示創(chuàng)建自定義路由事件:
1、新建用戶(hù)控件,添加一個(gè)Button按鈕,添加按鈕的Click事件,XAML代碼如下:
<UserControl x:Class="CustomWpfRouteEvent.RouteEventControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Button Height="30" Width="100" Content="調(diào)用路由事件" Click="Button_Click"></Button> </Grid> </UserControl>
2、在用戶(hù)控件的后臺(tái)代碼中創(chuàng)建自定義路由事件,C#代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CustomWpfRouteEvent { /// <summary> /// RouteEventControl.xaml 的交互邏輯 /// </summary> public partial class RouteEventControl : UserControl { public RouteEventControl() { InitializeComponent(); } //1、聲明并注冊(cè)路由事件,使用冒泡策略 public static readonly RoutedEvent MyClientEvent = EventManager.RegisterRoutedEvent("MyClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RouteEventControl)); //2、通過(guò).NET事件包裝路由事件 public event RoutedEventHandler MyClick { add { AddHandler(MyClientEvent, value); } remove { RemoveHandler(MyClientEvent, value); } } /// <summary> /// 3、使用按鈕的單擊事件激發(fā)路由事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, RoutedEventArgs e) { RoutedEventArgs arg = new RoutedEventArgs(); arg.RoutedEvent = MyClientEvent; RaiseEvent(arg); } } }
3、在主界面中引入新創(chuàng)建的用戶(hù)控件,使用自定義的路由事件MyClick,并為MyClick事件編寫(xiě)調(diào)用的方法,XAML代碼如下:
<Window x:Class="CustomWpfRouteEvent.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:u="clr-namespace:CustomWpfRouteEvent" Title="演示自定義路由事件" Height="350" Width="525" WindowStartupLocation="CenterScreen"> <Grid> <u:RouteEventControl MyClick="RouteEventControl_MyClick"></u:RouteEventControl> </Grid> </Window>
4、RouteEventControl_MyClick方法的后臺(tái)代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CustomWpfRouteEvent { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void RouteEventControl_MyClick(object sender, RoutedEventArgs e) { MessageBox.Show("Hello:" + e.Source.ToString()); } } }
5、運(yùn)行程序,單擊Button按鈕,效果如下所示:
到此這篇關(guān)于WPF自定義路由事件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WebService出現(xiàn)"因 URL 意外地以 結(jié)束,請(qǐng)求格式無(wú)法識(shí)別"的解決方法
因 URL 意外地以“/GetReceivedInvoices”結(jié)束,請(qǐng)求格式無(wú)法識(shí)別。2009-01-01asp.net DropDownList自定義控件,讓你的分類(lèi)更清晰
記得上次做論壇,一個(gè)功能就是合并2個(gè)子板塊的主題,用級(jí)聯(lián)的2個(gè)DropDownList也是可以完成,那樣我們要合并的時(shí)候總共就有4個(gè)DropDownList控件,覺(jué)得界面友好2011-10-10Asp.net MVC 對(duì)所有用戶(hù)輸入的字符串字段做Trim處理的方法
這篇文章主要介紹了Asp.net MVC 如何對(duì)所有用戶(hù)輸入的字符串字段做Trim處理,需要的朋友可以參考下2017-06-06ASP.NET Core優(yōu)雅的在開(kāi)發(fā)環(huán)境保存機(jī)密(User Secrets)
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core如何優(yōu)雅的在開(kāi)發(fā)環(huán)境保存機(jī)密User Secrets,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05ASP.NET實(shí)現(xiàn)單點(diǎn)登陸(SSO)適用于多種情況
這篇文章主要介紹了ASP.NET在不同情況下實(shí)現(xiàn)單點(diǎn)登陸(SSO)的方法,在同主域但不同子域之間實(shí)現(xiàn)單點(diǎn)登陸等等2014-09-09對(duì)Entity?Framework?Core進(jìn)行單元測(cè)試
這篇文章介紹了對(duì)Entity?Framework?Core進(jìn)行單元測(cè)試的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03asp.net調(diào)用飛信免費(fèi)發(fā)短信(測(cè)試有效)
這篇文章主要介紹了asp.net如何調(diào)用飛信免費(fèi)發(fā)短信,記得要開(kāi)通飛信把對(duì)方加為好友才能發(fā),需要的朋友可以參考下2014-05-05JavaScript用JQuery呼叫Server端方法實(shí)現(xiàn)代碼與參考語(yǔ)法
從Javascript客戶(hù)端用JQuery呼叫Server端的方法,這也是一個(gè)大膽的嘗試,本人做了演示動(dòng)畫(huà)以及參考語(yǔ)法,感興趣的朋友可以參考下,希望本人對(duì)你有所幫助2013-01-01關(guān)于Metalama使用Fabric操作項(xiàng)目或命名空間的問(wèn)題
Metalama是一個(gè)基于微軟編譯器Roslyn的元編程的庫(kù),可以解決我在開(kāi)發(fā)中遇到的重復(fù)代碼的問(wèn)題,這篇文章主要介紹了Metalama使用Fabric操作項(xiàng)目或命名空間,需要的朋友可以參考下2022-04-04