I' ve created a customer control MenuLink.cs in silverlight 2beta2, and the code like this:
- public class MenuLink : Control
- {
- public MenuLink( )
- {
- DefaultStyleKey = typeof(MenuLink);
- this.Loaded += new RoutedEventHandler(MenuLink_Loaded);
- }
- void MenuLink_Loaded(object sender, RoutedEventArgs e)
- {
- }
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- }
- }
There is a canvas named 'canMenu' in page 'Page.xaml', and i add the menulink into the canvas dynamicly:
- public Page()
- {
- InitializeComponent();
- this.Loaded += new RoutedEventHandler(Page_Loaded);
- }
- void Page_Loaded(object sender, RoutedEventArgs e)
- {
- MenuLink ml = new MenuLink(true);
- canMenu.Children.Clear();
- canMenu.Children.Add(ml);
- }
In this situation, the event in MenuLink.cs are called by this orders:
- 1. public MenuLink( bool a )
- 2. void MenuLink_Loaded(object sender, RoutedEventArgs e)
- 3. public override void OnApplyTemplate()
But if i add the menulink after downloading a xml file in page.cs like this:
- public Page()
- {
- InitializeComponent();
- this.Loaded += new RoutedEventHandler(Page_Loaded);
- }
- void Page_Loaded(object sender, RoutedEventArgs e)
- {
- WebClient webclient = new WebClient();
- webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
- webclient.DownloadStringAsync(new Uri("a.xml",UriKind.Relative));
- }
- void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
- {
- MenuLink ml = new MenuLink(true);
- canMenu.Children.Clear();
- canMenu.Children.Add(ml);
- }
Then the order of events/methods called in MenuLink.cs like this:
- 1. public MenuLink( bool a )
- 2. public override void OnApplyTemplate()
- 3. void MenuLink_Loaded(object sender, RoutedEventArgs e)
I really don't know why?????