Book notes on. NET4.0 Object-Oriented Programming-Chapter 1 event-driven

Source: Internet
Author: User

Chapter 4 event and event-driven

【Abstract】 event-driven is an important feature of object-oriented software systems. This chapter describes the. NET event processing mechanism, basic methods for custom events, and WPF route events.

Section 1 Analysis and Application of. NET Event Processing Mechanism

1. Concept: From the object-oriented perspective, an event is a message sent by an object, which is a signal that notifies other objects of an event.

The object that triggers the event is called the event source. The response object to this event is called the responder. The responder must provide an event response method.

2. essence: Events and delegation are essentially the same, but there are also some differences:

A. events can be directly triggered by external parties when delegation is used, and events must be triggered by the event source object (indirectly ).

B. Use Delegate as the extension of the Delegate type and EventHandle as the post-Delegate of the event, which is a standard naming rule.

3. the. NET event is an application of the observer mode in the delegate and is implemented based on the. NET specification, reflecting better coupling and flexibility.

4. In an event application, you can implement a dynamic event response method at runtime, or you can implement a method to respond to events of multiple objects.

5. Basic Methods for customizing events:

A. Create an event-specific delegate that defines the signature of the event response method.

B. Use the event keyword to define an event field for the object.

C. Inspire events in the right place.

Prepare the Event Response Method and mount it.

Section 2 routing events in WPF

1. WPF events have their own characteristics. They support three types of events.
A. Direct events: events are only triggered on one event source, and they are not "spread" or "spread ".
B. Tunnel events: events are automatically propagated down the logic tree. For example, PreviewKeyDown, which is transmitted from the logical root element to the event source element.
C. Bubble events: events are automatically transmitted "up" in the logic tree. For example, MouseDown. It is transmitted from the event source element to the logical root element.

2. Additional Events:
When the parent element does not have an event type specific to the child element, you can use the "additional event" mechanism of WPF to respond to the routing event of the child element.
<Grid ButtonBase. Click = "GridButton_Click">
......
</Grid>
Multiple button objects can be placed in the Grid. With additional events, we only need to define an Event Response Method in one place.

3. keyboard routing events:
A. PreviewKeyDown: Tunnel type, which is triggered when A key is pressed.
B. KeyDown: bubble type, same as above
C. PreviewTextInput: Tunnel type. The key ends and the element receives the character corresponding to the key (the control key does not trigger this event)
D. TextInput: bubble type, same as above
E. PreviewKeyUp: Tunnel Type, triggered when the button is released
F, KeyUp: bubble type, same as above.
That is, "Preview" is used as the identifier before the tunnel event name.

[Summary]: the. NET event model is built on the delegate mechanism, and the event is the encapsulation of the delegate.

Related Article

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.