WPF Concept 1: loaded event and initialized event of frameworkelement

Source: Internet
Author: User
Loaded event and initialized event of WPF frameworkelement

In WPF, The frameworkelement class defines two events: loaded and initialized events. When the control is loaded to
When these two events occur one after another, what is the difference between the two events? When developing custom controls, how should we use these two events?
Summary:

    • Initialized event: This event indicates that the frameworkelement has been created,
      And all its attributes have been set. Normally, the sub-element event is triggered before the parent element. When the element receives the event, only
      Indicates that its sub-tree has been initialized, and its parent element has not been initialized. When the Xmal sub-tree of the element is loaded
      This event is triggered. In this case, the isinitialized attribute of frameworkelement is true.
    • Loaded event: This event indicatesFrameworkelement
      Not only is it created, initialized, And the element has been loaded to virtualtree, then the element will be render. Slave root section
      The event is triggered on the sub-element in broadcast mode. At this time, the isloaded attribute of frameworkelement is
      True.
Initialized event

The initialized event indicates that all attributes of the current element have been set. In WPF
Frameworkelement implements the isupportinitialize interface. When the endinit method is called
The isinitialized attribute is true and then triggeredInitialized
Event.

 
The isupportinitialize interface allows developers
After begininit, the setting of some attributes is postponed until the endinit time is unified.Proceed
Set multiple attributes. ExploitationIsupportinitialize InterfaceIt can solve the problem caused by the attribute setting sequence and mutual constraints between attributes.

 
The XAML loader of WPF already supportsIsupportinitialize interface. After an object is created (the Start Node of the object after it is parsed ),
The begininit method calls the endinit method when the XAML tree of the object is loaded (the end node of the object is parsed.
Example:

 
<Button width = "100"> Hello world! </Button>

When a button is created, begininit is called, and the width attribute is set., The content attribute is set, and the endinit method is called.

Another thing is that the element's implicit style will not be applied before initialized, as shown in the following example:
Child: the background is defined as blue in the style of the button type, but the button1 objectInBefore initialized, its background is null.

 
<Page xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <page. resources> <style targettype = "button"> <setter property = "background" value = "blue"/> </style> </page. resources> <button name = "button1"> clack </button> </Page>

If you useCodeTo create a frameworkelement object without calling the begininit and endinit methods of the object. When the object is added to the visualtree, the initialized event is triggered.

The initialized event is triggered from the bottom up, from the parent to the child. Normally, the parent object should be initialized only after the sub-object is initialized.

Loaded event

When the loaded event of frameworkelement is triggered, the object has been initialized and
Will be drawn. Developers can perform some applications in this event.Program.


The initialized event can also perform initialization, but some attributes of the element in this event are inaccurate, such as actualwidth and
Actualheight, because the object is only initialized and has not started to calculate actualwidth and actualheight, some data binding
The calculation has not started.

The loaded event is usually triggered before the first render, And the element has been layout.
The binding is also calculated. It should be noted that if you have entered layout in loaded, this event may be re-triggered before render.
.

The loaded event is triggered from top to bottom in broadcast mode. It is triggered from the root node and from the parent node to the child. WhileInitialized
The event is triggered from the bottom up, first child and then parent.

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.