欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF自定義路由事件

 更新時(shí)間:2022年02月25日 16:38:32   作者:.NET開(kāi)發(fā)菜鳥(niǎo)  
這篇文章介紹了WPF自定義路由事件的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

與依賴(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)文章

最新評(píng)論