The run order about loaded event and onapplytemplate in custom control of Silverlight 2 beta 2

Source: Internet
Author: User

I 've created a customer control menulink. CS in Silverlight 2beta2, and the code like this:

  1. Public class menulink: Control
  2. {
  3. Public menulink ()
  4. {
  5. Defaultstylekey = typeof (menulink );
  6. This. Loaded + = new routedeventhandler (menulink_loaded );
  7. }
  8. Void menulink_loaded (Object sender, routedeventargs E)
  9. {
  10. }
  11. Public override void onapplytemplate ()
  12. {
  13. Base. onapplytemplate ();
  14. }
  15. }

There is a canvas named 'menu 'in page 'page. XAML', and I add the menulink into the canvas dynamicly:

  1. Public page ()
  2. {
  3. Initializecomponent ();
  4. This. Loaded + = new routedeventhandler (page_loaded );
  5. }
  6. Void page_loaded (Object sender, routedeventargs E)
  7. {
  8. Menulink ML = new menulink (true );
  9. Canmenu. Children. Clear ();
  10. Canmenu. Children. Add (ML );
  11. }

In this situation, the event in menulink. CS are called by this orders:

  1. 1. Public menulink (bool)
  2. 2. Void menulink_loaded (Object sender, routedeventargs E)
  3. 3. Public override void onapplytemplate ()

But if I add the menulink after downloading a XML file in page. CS like this:

  1. Public page ()
  2. {
  3. Initializecomponent ();
  4. This. Loaded + = new routedeventhandler (page_loaded );
  5. }
  6. Void page_loaded (Object sender, routedeventargs E)
  7. {
  8. WebClient = new WebClient ();
  9. WebClient. downloadstringcompleted + = new downloadstringcompletedeventhandler (webclient_downloadstringcompleted );
  10. WebClient. downloadstringasync (New uri ("A. xml", urikind. Relative ));
  11. }
  12. Void webclient_downloadstringcompleted (Object sender, downloadstringcompletedeventargs E)
  13. {
  14. Menulink ML = new menulink (true );
  15. Canmenu. Children. Clear ();
  16. Canmenu. Children. Add (ML );
  17. }

Then the order of events/methods called in menulink. CS like this:

  1. 1. Public menulink (bool)
  2. 2. Public override void onapplytemplate ()
  3. 3. Void menulink_loaded (Object sender, routedeventargs E)

 

I really don't know why ?????

 

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.