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

學無先后,達者為師

網站首頁 編程語言 正文

WPF自定義路由事件_實用技巧

作者:.NET開發菜鳥 ? 更新時間: 2022-04-29 編程語言

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

一、聲明路由事件變量并注冊

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

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代碼如下:


    
        
    

2、在用戶控件的后臺代碼中創建自定義路由事件,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、聲明并注冊路由事件,使用冒泡策略
        public static readonly RoutedEvent MyClientEvent = EventManager.RegisterRoutedEvent("MyClick",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RouteEventControl));

        //2、通過.NET事件包裝路由事件
        public event RoutedEventHandler MyClick
        {
            add
            {
                AddHandler(MyClientEvent, value);
            }
            remove
            {
                RemoveHandler(MyClientEvent, value);
            }
        }

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

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


    
        
    

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

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、運行程序,單擊Button按鈕,效果如下所示:

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

欄目分類
最近更新