My Silverlight series (7) -- control loaded event and onapplytemplate

Source: Internet
Author: User

First, I will introduce the loaded event and the onapplytemplate method, the two protagonists of today's debut, with no particular ranking (warm applause ~).

Microsoft explained the loaded event as follows:

Code
// Summary:
// Occurs when the element has completed layout passes, has rendered, and is
// Ready for interaction.
Public   Event Routedeventhandler loaded;

I don't need to translate these words. This means that the event occurs at the point at which the layout of the control has been completed and the user input can be reflected.
Then let me talk about this applytemplate (). Let's look at Microsoft's instructions: Code
// Summary:
// When overridden in a derived class, is invoked whenever application code
// Or internal processes (such as a rebuilding layout pass) call system. Windows. Controls. Control. applytemplate ().
[Securitysafecritical]
Public   Virtual   Void Onapplytemplate ();

These birds mean that when you reload a subclass, this method willCodeOr an internal operation (such as re-constructing the layout) is called when the applytemplate () method is called.

So the question is, when a control with a template is loaded, will it applytemplate first or trigger the loaded event first?
The answer is not necessarily. For this reason, I specifically wrote code for testing. First, I overloaded two general controls and then overwritten their onapplytemplate () method. The Code is as follows:

Code
Public   Class Mybutton: button
{
Public   Override   Void Onapplytemplate ()
{
Debug. writeline (
" Button applied "   + Datetime. Now. tostring ( " O " ));
Base . Onapplytemplate ();
}
}

Public   Class Mycheckbox: checkbox
{
Public   Override   Void Onapplytemplate ()
{
Debug. writeline (
" Checkbox applied "   + Datetime. Now. tostring ( " O " ));
Base . Onapplytemplate ();
}
}

 

Then in XAML, I define it as follows:

Code
< Usercontrol X: Class = "Silverlightapplication2.page"
Xmlns
= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
Xmlns: ABCD
= "CLR-namespace: silverlightapplication2"
Xmlns: x
= "Http://schemas.microsoft.com/winfx/2006/xaml"  
Width
= "400" Height = "300" Loaded = "Usercontrol_loaded" >
< Grid X: Name = "Layoutroot" Background = "White" >
< ABCD: mybutton Loaded = "Mybutton_loaded" Horizontalcontentalignment = "Center" >
< ABCD: mybutton. Content >
< ABCD: mycheckbox Loaded = "Insidecheckbox_loaded" />
</ ABCD: mybutton. Content >
</ ABCD: mybutton >
< ABCD: mycheckbox Loaded = "Outsidecheckbox_loaded" />
</ Grid >
</ Usercontrol >

In the background code:

Code
Private void mybutton_loaded (Object sender, routedeventargs E)
{
Debug. writeline ("button loaded at" + datetime. Now. tostring ("O "));
}

Private void insidecheckbox_loaded (Object sender, routedeventargs E)
{
Debug. writeline ("Inside checkbox loaded at" + datetime. Now. tostring ("O "));
}

Public override void onapplytemplate ()
{
Debug. writeline ("Page applied at" + datetime. Now. tostring ("O "));
Base. onapplytemplate ();
}

Private void usercontrol_loaded (Object sender, routedeventargs E)
{
Debug. writeline ("Page loaded at" + datetime. Now. tostring ("O "));
}

Private void outsidecheckbox_loaded (Object sender, routedeventargs E)
{
Debug. writeline ("outside checkbox loaded at" + datetime. Now. tostring ("O "));
}

Screenshot of vs during the final running:

Maybe you haven't understood the code and the illustration yet, so let me explain it. First, the onapplaytemplate () method of the page class is not executed because it does not have a template. Then, from the loaded events of the button, checkbox, and page, the checkbox and button are used as the child controls of the page, load is completed first, and the page serving as the parent needs to load later. Then, the loaded events of the button and checkbox are earlier than applytemplate. Finally, the checkbox loading as the content of the button is the latest, and its applytemplate is earlier than loaded.

The last one is why I say loaded and applytemplate are not necessarily late. From the above sequence, we can see at least three points: first, the Child Control always triggers the loaded event before the parent control; second, the contentpresender control is applied, if its content is also a control, the control is later than the parent control loaded. Third, in the second case, the applytemplate of the Control of content is earlier than loaded, in other cases, the loaded event of the control is earlier than applytemplate.

Some of the things Microsoft has always done are incredible. For example, applytemplate is later than loaded. I tested the same things on WPF. applytemplate is always earlier than loaded, which is reasonable, after applying the template, I will tell others that I have loaded the template. This is a logic that is available to normal people, but in Silverlight, most of the cases are reversed. Of course, I don't believe that the buddy who designed Silverlight had to reverse the call sequence when writing the architecture. Microsoft always had a reason for doing so, but what is the reason? Is there a difference between drawing on the desktop and on the browser? I don't know about this. Maybe only Microsoft's talents who write OS and IE kernel know this ......

Finally, let me talk about the problem of the Silverlight mechanism, that is, for example, I write a control and then in generic. in XAML, a template is written for this control. I need to call the gettemplatechild method to override the onapplytemplate method to obtain the instance of the Child control in the template. Because this process is later than the loaded event, therefore, if I assign values to some attributes of the Child control in the control template when the loaded event is triggered, the nullreferenceexception is thrown because the child control has not been initialized. The solution is not none. We only need to manually set the applytemplate method, but this destroys the default behavior of the class. I think it is not very good.

For the above points, the test is too hasty, and some problems may not be found in it. You are also welcome to correct me.

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.