WPF Browser throws error

1

I am calling javascript functions from C# using the WPF variant of the browser control via InvokeScript.

I can call my function once without any problems. But when I call it a second time, it throws the following error :

Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

The Code I am using is the following :

this.browser.LoadCompleted += (sender, args) =>
{
   this.browser.InvokeScript("WriteFromExternal", new object[] { "firstCall" }); // works
   this.browser.InvokeScript("WriteFromExternal", new object[] { "secondCall" }); // throws error
};

The javascript function is :

function WriteFromExternal(message) {
   document.write("Message : " + message);
}

I can call C# functions from the page via javascript just fine and invoke from C#, just can't invoke a second time. Regardless of what function I call.

I do not understand why it would fail the second time.

Thank you

Edit :

Did the following test (javascript) :

        function pageLoaded() {
            window.external.tick();
            window.external.tick();
            window.external.tick();
        }
        window.onload = pageLoaded;
        function WriteFromExternal(message) {
            document.write("Message : " + message);
        }

And this is the C# side :

            private int i = 0;
            public void tick()
            {
                invoke("WriteFromExternal", new object[] { "ticked"+ i++ });
            }
            public static void invoke(string method, object[] parameters)
            {
                mainInterface.browser.InvokeScript(method, parameters);
            }

And still throws the same error (after the first call), this suggests that it does not matter from where it is called, invoking the function from C# will throw this error if done more than once.

c#
wpf
browser
asked on Stack Overflow Jul 18, 2013 by Joao Carlos • edited Jul 18, 2013 by Joao Carlos

2 Answers

0

It's been a while since I did something similar, but from what I remember your code looks correct. However, I do remember using a slightly different pattern in my project. Instead of delegating back to a JS method on the page I would make my ScriptingHost methods return values

EX: C#:

public string tick()
{
   return "some stuff";
}

var msg = window.external.tick();
document.write(msg);

If you have more complex objects than simple strings you can serialize them to JSON and parse them into an object on the JS side.

var jsonObj = JSON.parse(window.external.someMethod());

Not sure if you have the luxury of being able to change your method signatures in your scripting object, but it's at least an alternative approach.

Also, in your current implementation, have you tried to do something other than document.write? Do you get the same error if you display an alert box?

answered on Stack Overflow Jul 18, 2013 by TGH
0

I assume you did the same as me and put your scripts in the body. For some reason when you call document.write from wpf it completely overwrites the document. If instead of using document.write you append a child it works fine. So change your JavaScript function to be:

 window.WriteFromExternal = function (message) {
   var d = document.createElement("div")
   d.innerHTML= "Message : " + message;
   document.body.appendChild(d);
}
 
 // call from c#
 WriteFromExternal("test")

answered on Stack Overflow Feb 3, 2017 by David Wilton

User contributions licensed under CC BY-SA 3.0