What is the order: SilverlightPlugin.OnPluginLoaded, App.Application_Startup and Page.Page_Loaded and MyCustomControl.OnApplyTemplate


  • Here is the setup.

    3 Projects - one for SL application, one for the hosting Web site, one for the custom control. So the App only has one Page, the default. Page xaml has the custom control I created. The custom control is skinnable using Style with the Tempalte Setter. The custom control is also scriptable. Web site has an ASPX page that uses Silverlight plugin to download the XAP. The idea is that the SL control can be "drawn at" from Javascript.Therfor, the ASPX page also has some javascript logic to "draw" on the control.

    The way I wired it is this. the plugin OnPluginLoaded calls the Javascript function that gets the reference to the scriptable class and calls the drawing functions.

    The effect is NullReferenceException. That is because inside the control RootElement was not yet instantiated. That is because MyCustomControl.OnApplyTemplate has not yet been called. it is inside OnApplyTemplate I instantiate ElementRoot

    ElementRoot = GetTemplateChild(ElementRootName) as FrameworkElement;

    So here is the short question: what shold I do? How do I make sure the page controls are all loaded before the javascript layers kicks in. If anything else fails, I'll wire up a MyConstomControl.Ready event to some Javascript function.

    The long question - what is the inner mechanics? I assumed without actually knowing, that plugin loads, it downloads the XAP, creates teh App, hence App.App() is called, then it somehow calls Application_Startup and Page() gets created, where its Loaded event is routed to Page_Loaded handler

    public Page()



    this.Loaded += new RoutedEventHandler(Page_Loaded);


    I further assumed it is only then the event is fired to trigger Javascript.



    Tuesday, March 18, 2008 12:35 PM


  • Hello, the order is: SilverlightPlugin.OnPluginLoaded, Application.Startup, Page.Loaded, and finally, Control.OnApplyTemplate. So in your case, yes, you should create a custom event to use in JavaScript after OnApplyTemplate is completed.

    Thursday, March 20, 2008 12:47 AM