views:

31

answers:

1

I am working on a project where I have a lot of interaction between JavaScript and managed code. In fact, I need the JS application to interface with the Silverlight application in the page right from the beginning. So I need the Silverlight application to load before the JS code gets executed.

But while doing so, most of the times I get the error that the object was not found as the Silverlight application has not yet been loaded. So I need the Silverlight to load before the JS executes.

Is there a way I can put a stop on the JS application until the Silverlight loads and then start executing?

A: 

First, you need to add an event handler to the Loaded event of your app, from that event handler you can call the javascript function like this:

using System.Windows.Browser;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        HtmlPage.Window.CreateInstance("SomeFunction", new string[] { "parameter1", "parameter2" });
    }
}

}

Note that you need the System.Windows.Browser namespace to use HtmlPage

Arturo Molina
Forgot to mention, I got this from Tim Heuer's post: http://timheuer.com/blog/archive/2008/03/09/calling-javascript-functions-from-silverlight-2.aspx
Arturo Molina