In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "WPF how to customize routing events", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "WPF how to customize routing events" this article.
Routing event model
In the traditional simple event model, when the message is fired, the message is subscribed through the event and then handed over to the corresponding person of the event, and the corresponding person of the event uses the handler of the event to make the response, so there is a problem. Events within the user control cannot be subscribed by the outside world, because the host of the event must be able to directly access the responder of the event.
There is no direct explicit subscription relationship between the event owner of the routed event and the counterpart of the event, while the owner of the event is only responsible for firing the event, and it does not know who will respond to the event. the responder of the event has an event listener to listen for the event, and when such an event is delivered to this event, the responder uses the event handler to respond to the event and determines whether the event continues to be delivered. For example, as in the previous program, the event starts to fire after clicking "click me", and then the event is passed on the control tree, and the responder of the event installs a listener, and when the monitor hears the event, it responds and decides whether the event continues to pass.
Now that you have learned about routing events, this section will learn how to customize routing events.
[analysis code]
Before demonstrating the code, let's take a look at the Click routing event source code of the Button button and learn how to define routing events from the source code.
In ButtonBase, there are four codes related to Click routing events:
Code one
Code two
Code three
Code four
The first paragraph declares the routing event object ClickEvent, needless to say.
The second section is a wrapper that declares the ClickEvent routing event object, which is similar to the get,set of the attribute, making it easy for us to attach the handler of the routed event to the routed event from the outside. When the "+ =" operation is performed on the outside, the add block is executed internally, and event handling is appended to the Click routing event, while when the "- =" operation is performed, the contents of the remove block are executed.
The third paragraph is to build a Click routing event object in the construction method. Similar to creating a dependent object, the creation of a routed event object is not a direct new, but is registered through the RegisterRoutedEvent method of the EventManager class. The first parameter of this method is the name of the routed event. Microsoft agrees that the routed event name should be the same as the wrapper name of the routed event object and the word that the routing event object removes the Event suffix. The second parameter refers to the policy for routing events, that is, the form of event propagation. There are three enumerations as follows:
RoutingStrategy.Tunnel: tunnel, which means that events are routed from the outermost control to the control itself, like a tunnel from the window control to the control that currently triggers the event.
RoutingStrategy.Bubble: bubble, as opposed to tunnel, which propagates upward from the control that triggers the event until the top layer stops
RoutingStrategy.Direct: cut-through, like the original event model, non-routing, direct to the event handler.
The third parameter is to specify the type of event handler for the routed event, the fourth parameter is to specify the host type of the routed event object, and the fourth parameter, together with the first parameter, is used for the internal creation of the routed event object: building a hash code, determining the uniqueness of the routed event object, consistent with the dependency attribute, and not registering two routed event objects with the same name in one class.
The fourth paragraph is the method of firing Click routing events, and the event parameters are handled in this method, which is the source of routing event propagation.
[custom routing event]
Let's create our own Click routing event based on ButtonBase based on the above syntax:
Public class MyRoutedEventArgs: RoutedEventArgs {public MyRoutedEventArgs (RoutedEvent routedEvent, object source): base (routedEvent, source) {} public string RoutedMessage {get; set;}} public class MyButton: ButtonBase {public static readonly RoutedEvent MessageEvent = EventManager.RegisterRoutedEvent ("Message", RoutingStrategy.Bubble, typeof (EventHandler), typeof (MyButton)) Public event RoutedEventHandler Message {add {this.AddHandler (MessageEvent, value);} remove {this.RemoveHandler (MessageEvent, value);}} protected override void OnClick () {base.OnClick () MyRoutedEventArgs eventArgs = new MyRoutedEventArgs (MessageEvent, this) {RoutedMessage = "Custom routing event triggered"}; this.RaiseEvent (eventArgs);}}
In the above code, I created a MyButton and declared a MessageEvent routing event object. It is worth noting that I used the third parameter of RegisterRoutedEvent:
Typeof (EventHandler)
Instead of:
Typeof (RoutedEventHandler)
Because the parameters of RoutedEventHandler are not consistent with my custom event parameters, I need to specify my event parameter type using the generic version of EventHandler. Here is the declaration of RoutedEventHandler:
The parameter type is RoutedEventArgs, while I am using a custom MyRoutedEventArgs type.
The code for the XAML section and the event handler is:
Set the MyButton Message event listener and handler on the outer Grid.
The running effect is as follows:
Click "Hello" and the MessageBox prompt "Custom routing event triggered" pops up.
The above is all the content of the article "how to customize routing events in WPF". 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.