How to handle Javascript events via WebBrowser control for WinForms

后端 未结 3 1920
粉色の甜心
粉色の甜心 2020-12-03 01:27

I have read WebBrowser Control from .Net — How to Inject Javascript, Is it possible to call Javascript method from C# winforms and many others. Those examples were returns f

3条回答
  •  旧巷少年郎
    2020-12-03 02:06

    Calling C# from JavaScript

    Simply put, you can expose a C# object to the WebBrowser that the JavaScript can call directly The WebBrowser class exposes a property called ObjectForScripting that can be set by your application and becomes the window.external object within JavaScript. The object must have the ComVisibleAttribute set true

    C#:

     [System.Runtime.InteropServices.ComVisibleAttribute(true)]
        public class ScriptInterface
        {
            public void callMe()
            {
                … // Do something interesting
            }
        }
    
        webBrowser1.ObjectForScripting = new ScriptInterface();
    

    Javascript:

    window.external.callMe();
    

    Calling JavaScript in a WebBrowser control from C#

提交回复
热议问题