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.