In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample Analysis of WPF Custom routing events", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of WPF Custom routing events".
Declare routing event variables and register
Define a read-only static variable field RouteEvent class to declare a variable, and then register the routed event with the event system using the RegisterRoutedEvent () method of EventManager, which is signed as follows:
Public static RoutedEvent RegisterRoutedEvent (string name, RoutingStrategy routingStrategy, Type handlerType, Type ownerType)
This method takes four parameters:
The first parameter, name, represents the name of the routed event in the WPF event system.
The second parameter, routingStrategy, is an enumerated value of type RoutingStrategy, indicating the routing policy for routed events. There are three strategies:
The first Bubble is the bubbling strategy, which is passed from the trigger point to the root node to the outermost layer.
The second is that Direct is the same as traditional events.
The third is the tunneling strategy, which is contrary to the bubbling strategy and is passed down.
The third parameter, handlerType, is used to indicate the type of event handler.
The fourth parameter, ownerType, is used to indicate the type of event that owns the route.
The RegisterRoutedEvent () method of EventManager returns an instance of type RoutedEvent. Normally, the instance will be saved by a public static readonly field.
Second, routing events through standard .NET event wrappers
The event wrapper uses the AddHandler method to add the caller that routes the event, and then uses RemoveHandler to remove the caller that has been added.
Third, create a method that can trigger routing events
Demonstrates creating a custom routing event:
1. Create a new user control, add a Button button, and add the Click event of the button. The XAML code is as follows:
2. Create a custom routing event in the backend code of the user control. The C# code is as follows:
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 interaction logic / public partial class RouteEventControl: UserControl {public RouteEventControl () {InitializeComponent ();} / / 1, declare and register routing events, using bubbling policy public static readonly RoutedEvent MyClientEvent = EventManager.RegisterRoutedEvent ("MyClick", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (RouteEventControl)) / / 2. Route event public event RoutedEventHandler MyClick {add {AddHandler (MyClientEvent, value);} remove {RemoveHandler (MyClientEvent, value) through .NET event wrapper }} / 3. Use the click event of the button to fire the routing event / private void Button_Click (object sender, RoutedEventArgs e) {RoutedEventArgs arg = new RoutedEventArgs (); arg.RoutedEvent = MyClientEvent; RaiseEvent (arg) }}}
3. Introduce the newly created user control into the main interface, use the custom routing event MyClick, and write the calling method for the MyClick event. The XAML code is as follows:
4. The background code of the RouteEventControl_MyClick method is as follows:
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 interaction logic / public partial class MainWindow: Window {public MainWindow () {InitializeComponent ();} private void RouteEventControl_MyClick (object sender, RoutedEventArgs e) {MessageBox.Show ("Hello:" + e.Source.ToString ());}
5. Run the program and click the Button button, and the effect is as follows:
The above is all the contents of the article "sample Analysis of WPF Custom routing events". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.