Search code examples
javascriptc#winformsexceptionawesomium

CreateGlobalJavascriptObject issues


I trying to use CreateGlobalJavascriptObject in my app, but have issues. Code:

WebCore.Initialize(WebConfig.Default);
webControl.Source = new Uri("file:///res/login.html");
webControl.Update();
JSObject myGlobalObject = webControl.CreateGlobalJavascriptObject("object");

Output: System.AccessViolationException in Awesomium.Windows.Forms.dll.

After that I trying just to put webView.CreateGlobalJavascriptObject("object"); in the default Awesomium WinForms sample project:

    public WebForm()
    {
        WebSession session = InitializeCoreAndSession();
        InitializeComponent();
        InitializeView( WebCore.CreateWebView( this.ClientSize.Width, this.ClientSize.Height, session ) );
        webView.CreateGlobalJavascriptObject("object");
    }

Output: System.NullReferenceException in Awesomium.Core.dll.

How I can resolve these issues?


Solution

  • This very same question has been answered already on answers.awesomium.com. I'll post the answer here as well so that it might help others:

    You have to wait until the frame is loade. Use the IWebView.LoadingFrameComplete event:

    webView.LoadingFrameComplete += OnLoadingFrameComplete;
    

    And your event handler could look similar to this:

    private void OnLoadingFrameComplete(object sender, FrameEventArgs e)
    {
       JSObject myGlobalObject= webControl.CreateGlobalJavascriptObject("myObject");
    }