Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

javascript - C# WebBrowser control: window.external access sub object

when assigning an object to the ObjectForScripting property of a WebBrowser control the methods of this object can be called by JavaScript by using windows.external.[method_name]. This works without problems.

But how I need to design this C# object when I have a JavaScript function like this (accessing a sub object): window.external.app.testfunction();

I tested it with following C# object assigned to the ObjectForScripting property:

[ComVisible(true)]
public class TestObject
{
    public App app = new App();
}

public class App
{
    public void testfunction()
    {
    }
}

But this unfortunately does not work and leads to a JavaScript error saying "function expected".

Any idea on how the C# object has to look like that this JavaScript command is working?

Thank you for any tips on that

Andreas

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you use InterfaceIsIDispatch-based interfaces to expose the object model from C# to JavaScript:

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IApp
    {
        void testFunction();
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ITestObject
    {
        IApp App { get; }
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(ITestObject))]
    public class TestObject: ITestObject
    {
        readonly App _app = new App();

        public IApp App
        {
            get { return _app; }
        }
    }

    [ComVisible(true)]
    public class App : IApp
    {
        public void testFunction()
        {
            MessageBox.Show("Hello!");
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser1.ObjectForScripting = new TestObject();
            this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
            this.webBrowser1.Navigate("about:blank");
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.webBrowser1.Navigate("javascript:external.App.testFunction()");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...