. Net routing events

Source: Internet
Author: User

This articleArticleDescribes how to route events.

What is a routing event? Literally, it means that an event can be passed, and the meaning of a route is also understandable. The routing event actually means that the event will be passed back and forth with some changes. The routing event actually exists in the. net2.0 period, but it is not used in general development.

Since C #3.0, the routing event mechanism has been encapsulated. In fact, this implementation should be explained by another name. We can give the routing event an easy-to-understand name, "routing design mode of the event ". We all know that any large framework is written from a tiny basic syntax. platforms and Languages provide us with only something that can satisfy our daily needs; good things must be written and innovated by ourselves. I have a deep understanding of the use of events in common design patterns. Whether it is a master or not, you cannot use the framework or language that he uses. Instead, you need to know how well he understands the language he uses, can you write an efficient and simple framework? This is a master. This is also a problem that many beginners like to make.

Routing events are crucial in some complex system designs. For example, if I have an object that belongs to the container class, it is like a Windows application.ProgramForm, which is used to carry some other child forms. However, such a progressive design often appears. When we build an interface, we accumulate many small windows on this interface. These small windows pile up some smaller windows. When designing a hierarchical architecture, we need to consider that these objects cannot be buried too deeply, but we need to maintain the structure principle of the objects, as shown in;

1:

You may not be able to express the meaning of the image. There is a large object that has accumulated many small objects, and each small object has accumulated some small objects. We often see such hierarchies. Development on the. NET platform is basically based on the drag of controls, but these controls are encapsulated and contain some small objects. In the development of 2.0, the control does not support Event Routing. For example, when we subscribe to a control event, this event may be handled by the event above it; winform users often like to capture mouse-click events and write event triggers.Code. However, as long as the control is blocked by other controls, the control certainly cannot receive the mouse-click message sent to it by windows, because the event has no routing. The parent control above does not take into account that its children and grandchildren need this message. In WPF, The Event Routing Mechanism is provided. We can capture the events of the Child control.

In fact, the implementation principle is to pass down the event. The parent control needs to cyclically determine whether each sub-control is subscribed to related events. If the event sub-control captured by the parent control also needs, the event can be routed down;

2:

If we need the framework to support routing events, We need to extract the objects and encapsulate the objects that require routing events in the early stage; it is like starting with the base class of the control.

Let's look at a small example to help you understand the principle and use it when developing your own project.

I. container object code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // container class // </Summary> public class container {// <summary> // click the event with the mouse /Summary> public event eventhandler click; /// <summary> /// sub-object set /// </Summary> private list <child> childlist = new list <child> (); /// <summary> /// trigger the Click Event of the current object /// </Summary> Public void onclick () {Click ("the parent object receives the Click Event ", null); // triggers the event foreach (Child C in childlist) {C. onclick ();}} /// <summary> /// Add sub-object method /// </Summary> /// <Param name = "C"> </param> Public void add (Child C) {childlist. add (c );}}}

Ii. Sub-object code:

 
Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // sub-object // </Summary> public class child {// <summary> /// click the event with the mouse /Summary> public event eventhandler click; public void onclick () {Click ("the sub-object receives the Click Event", null );}}}

Iii. Call code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {class program {static void main (string [] ARGs) {// instantiate the container object container containerobject = new container (); // subscribe to the container Click Event containerobject. click + = new eventhandler (containerobject_click); // instantiate the subclass object child childobject = new child (); // subscribe to the subclass Click Event childobject. click + = new eventhandler (childobject_click); // Add the subclass to the container class containerobject. add (childobject); If (console. readline () = "startclick") {// triggers the container class click event. At this time, the event is routed to the sub-object; containerobject. onclick ();}} /// <summary> /// sub-object Click Event /// </Summary> /// <Param name = "sender"> This is the data transmitted from the sub-object </Param >/// <Param name = "E"> </param> static void childobject_click (Object sender, eventargs e) {console. writeline (sender as string); console. readline ();} /// <summary> /// container object Click Event /// </Summary> /// <Param name = "sender"> This is the data transmitted from the container object </param> // <Param name = "E"> </param> static void containerobject_click (Object sender, eventargs e) {console. writeline (sender as string); console. readline ();}}}

Finally:

The routing event is probably over, hoping to help your friends;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.