欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

路由事件:

1.路由事件一般使用的三种策略如下所示:
A.Bubble(冒泡模式):事件从自己激发一直传递到根元素;
B.Direct(直接模式):只有事件源才有机会相应事件(和传统事件一样);
C.Tunnel(隧道模式):事件从根元素传递到自己。
一般情况,WPF提供的输入事件都是以冒泡/隧道对实现的。隧道事件常常被称为Preview事件。

2.路由事件的注册方式
通过EventManager的RegisterRoutedEvent()函数向事件系统注册路由事件;
public static RoutedEvent RegisterRoutedEvent(string name, RoutingStrategy routingStrategy,
Type handlerType, Type ownerType);
参数解释:
第一个参数name表示事件在WPF事件系统中的名称;
第二个参数routingStrategy则标明了路由事件的路由原则;
第三个参数handlerType用来标明事件处理函数的类型;
第四个参数ownerType则用来标明拥有该路由事件的类型。
例如:
Control类注册MouseDoubleClick事件的代码如下:
public static readonly RoutedEvent MouseDoubleClickEvent =
EventManager.RegisterRoutedEvent("MouseDoubleClick", RoutingStrategy.Direct,
typeof(MouseButtonEventHandler), typeof(Control));

EventManager类还提供了一个RegisterClassHandler()函数,以为特定路由事件注册类处理程序。
该函数的原型如下:
public static void RegisterClassHandler(Type classType, RoutedEvent routedEvent,
Delegate handler, bool handledEventsToo);
第一个参数用来指定注册类处理函数的类型;
第二个参数则用来指定类处理函数所需要侦听的事件;
第三个参数则指明了类处理函数;
第四个参数设置为true则允许类处理函数能够处理被标记为已处理的路由事件。


3.创建自定义路由事件的步骤:
(1)声明并注册路由事件;
(2)为路由事件添加CLR事件包装;
(3)创建可以激发路由事件的方法。

复制代码
<Window x:Class="WPFDemos.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="GridRoot" >
        <Grid x:Name="gridA" Margin="10" Background="Blue">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Canvas x:Name="canvasLeft" Grid.Column="0" Background="Red" Margin="10">
                <Button x:Name="buttonLeft" Width="40" Height="100" Margin="30,135,357,76" Content="left">
                    <Button.RenderTransform>
                        <!--旋转度数-->
                        <RotateTransform Angle="270"/>
                    </Button.RenderTransform>
                </Button>
                <Canvas x:Name="canvasRight" Grid.Column="1" Background="Yellow" Margin="10">
                    <Button x:Name="buttonRight" HorizontalAlignment="Center" Content="right" Width="40" Height="100" Margin="138,105,325,106"  >
                        <Button.LayoutTransform >
                            <RotateTransform Angle="90"/>
                        </Button.LayoutTransform>
                    </Button>
                    <Button Content="test" Height="23" HorizontalAlignment="Left" Margin="80,217,0,0" Name="buttontest" VerticalAlignment="Top" Width="75" >
                        <Button.RenderTransform>
                            <!--旋转度数-->
                            <RotateTransform Angle="13"/>
                        </Button.RenderTransform>
                    </Button>
                </Canvas>
            </Canvas>
        </Grid>
    </Grid>
</Window>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Windows;
using System.Windows.Controls;
 
namespace WPFDemos
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            //当同一个UI中存在多个相同控件的相同事件时可考虑使用,例如计算器中的Button事件
            this.GridRoot.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.ButtonClicked));
        }
 
        private void ButtonClicked(object sender,RoutedEventArgs e)
        {
            MessageBox.Show("Test==" +(e.OriginalSource as FrameworkElement).Name);
        }
    }
}

posted on   sunwugang  阅读(723)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示