日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

WPF自定義路由事件_實(shí)用技巧

作者:.NET開(kāi)發(fā)菜鳥(niǎo) ? 更新時(shí)間: 2022-04-29 編程語(yǔ)言

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

一、聲明路由事件變量并注冊(cè)

定義只讀的靜態(tài)變量字段RouteEvent類來(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)中的名稱。
  • 第二個(gè)參數(shù)routingStrategy是RoutingStrategy類型的枚舉值,標(biāo)明了路由事件的路由策略,共三種策略:

    • 第一種Bubble是冒泡策略,這種模式是從觸發(fā)點(diǎn)向根節(jié)點(diǎn)傳遞,直到最外層。
    • 第二種是Direct就是傳統(tǒng)的事件一樣的。
    • 第三種是隧道策略,這和冒泡策略相反,向下傳遞。
  • 第三個(gè)參數(shù)handlerType用來(lái)標(biāo)明事件處理函數(shù)的類型。
  • 第四個(gè)個(gè)參數(shù)ownerType則用來(lái)標(biāo)明擁有該路由事件的類型。

EventManager的RegisterRoutedEvent()方法返回一個(gè)RoutedEvent類型的實(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、新建用戶控件,添加一個(gè)Button按鈕,添加按鈕的Click事件,XAML代碼如下:


    
        
    

2、在用戶控件的后臺(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
{
    /// 
    /// RouteEventControl.xaml 的交互邏輯
    /// 
    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);
            }
        }

        /// 
        /// 3、使用按鈕的單擊事件激發(fā)路由事件
        /// 
        /// 
        /// 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs arg = new RoutedEventArgs();
            arg.RoutedEvent = MyClientEvent;
            RaiseEvent(arg);
        }
    }
}

3、在主界面中引入新創(chuàng)建的用戶控件,使用自定義的路由事件MyClick,并為MyClick事件編寫(xiě)調(diào)用的方法,XAML代碼如下:


    
        
    

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
{
    /// 
    /// MainWindow.xaml 的交互邏輯
    /// 
    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按鈕,效果如下所示:

原文鏈接:https://www.cnblogs.com/dotnet261010/p/6372222.html

欄目分類
最近更新