Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Browser Control when using ObjectForScripting window.external.invoke() throws object doesn't support property or method

I'm trying to capture click event from a WPF Browser Control webpage. I've created an ObjectForScriptingHelper class:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class ObjectForScriptingHelper
{
    public void Invoke()
    {
        MessageBox.Show("Hello");
    }
}

And in the constructor of the WPF Window hosting the Web Browser control I'm using this helper class as the ObjectForScripting:

public MainWindow()
{
    InitializeComponent();
    StandardWpfBrowser.ObjectForScripting = new ObjectForScriptingHelper();    
}

I then have a Uri variable ViewPageUri which I navigate to:

StandardWpfBrowser.Navigate(ViewPageUri);

Then in the page displayed by the browser control I have referenced jquery, added a button the html page and added javascript:

$('button').click(function () {
    if (window.external === null)
        alert('ObjectForScripting is not set');
    else
    {
        alert('ObjectForScripting is set');
        window.external.Invoke();
    }
})

The else branch is being executed but error message object doesn't support property or method 'Invoke' is thrown.

All the tutorials and MSDN docs follow the approach above. Am I missing something?

like image 705
user2984303 Avatar asked Sep 24 '15 16:09

user2984303


1 Answers

In case anyone else has this same issue. It turned out that naming the method Invoke was the problem. Once I changed the method name it all worked as expected.

like image 103
user2984303 Avatar answered Nov 07 '22 01:11

user2984303