简体   繁体   English

在C#WPF mshtml WebBrowser控件中捕获自定义Javascript事件

[英]Capture a custom Javascript event in C# WPF mshtml WebBrowser control

I'm unable to capture a custom Javascript event in a C# WPF mshtml WebBrowser control. 我无法在C# WPF mshtml WebBrowser控件中捕获自定义Javascript事件。 I've created the example code below. 我在下面创建了示例代码。 A button click raises a custom event. 单击按钮会引发自定义事件。 I realise that there are easy ways to capture a button click event. 我意识到,有很容易的方法来捕获按钮单击事件。 I need to use a custom event. 我需要使用自定义事件。 I've used a button just for this example and testing. 我仅在此示例和测试中使用了按钮。 What am I doing wrong? 我究竟做错了什么?

XAML file: XAML文件:

<Window x:Class="WebEvent2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WebBrowser Name="webBrowser1" Loaded="webBrowser1_Loaded" LoadCompleted="webBrowser1_LoadCompleted"></WebBrowser>
    </Grid>
</Window>

C# file: C#文件:

namespace WebEvent2{
   public partial class MainWindow : Window{
      public MainWindow(){
         InitializeComponent();
      }

      private void webBrowser1_Loaded(object sender, RoutedEventArgs e{
         webBrowser1.Navigate(@"file:///D:/test.html");
      }

      private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e){
         try{
            var evtListener = new EventListener();
            var window = ((IHTMLDocument2)webBrowser1.Document).parentWindow as IHTMLWindow3;
            window.attachEvent("MyCustomEvent", evtListener);

            // Also not working:
            // ((HTMLDocument)webBrowser1.Document).attachEvent("MyCustomEvent", evtListener);
         }catch (UnauthorizedAccessException err){
            Console.WriteLine("OOPS: " + err);
         }
      }

      [ComVisible(true)]
      [ClassInterface(ClassInterfaceType.AutoDispatch)]
      public class EventListener{
         [DispId(0)]
         public void handler(IHTMLEventObj evt){
            MessageBox.Show("message received");
         }
      }
   }
}

HTML file: HTML档案:

<html>
<head>
  <title>Test page</title>
  <script>
    function sendCustomEvent() {
      var event;
      if (typeof(Event) === 'function') {
          event = new Event('MyCustomEvent');
      } else {
          event = document.createEvent('HTMLEvents');
          event.initEvent('MyCustomEvent', true, false);
      }

      document.dispatchEvent(event);
    }
  </script>
</head>
<body>
  <button onclick="sendCustomEvent()">Send custom event</button>
</body>
</html>

I was able to achieve the required functionality by using the WebBrowser controls 'ObjectForScripting' property. 通过使用WebBrowser控件的'ObjectForScripting'属性,我能够实现所需的功能。 Nice and simple. 漂亮又简单。

XAML remains the same as above. XAML与上面相同。

WebInterface.cs: WebInterface.cs:

namespace WebEvent2
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class WebInterface
    {
        public void callMe()
        {
            MessageBox.Show("hello");
        }
    }
}

MainWindow.xaml.cs: MainWindow.xaml.cs:

namespace WebEvent2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        WebInterface wi = new WebInterface();

        private void webBrowser1_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser1.ObjectForScripting = wi;
            webBrowser1.Navigate(@"file:///D:/test.html");
        }
    }
}

test.html: 的test.html:

<html>
<head>
  <title>Test page</title>
</head>
<body>
  <button onclick="window.external.callMe()">Send custom event</button>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM