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

时间:2018-11-22 06:08:38

标签: javascript c# wpf mshtml

我无法在Javascript mshtml WebBrowser控件中捕获自定义C# WPF事件。我在下面创建了示例代码。单击按钮会引发自定义事件。我意识到,有很容易的方法来捕获按钮单击事件。我需要使用自定义事件。我仅在此示例和测试中使用了按钮。我在做什么错了?

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#文件:

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文件:

<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>

1 个答案:

答案 0 :(得分:0)

通过使用WebBrowser控件的'ObjectForScripting'属性,我可以实现所需的功能。很好而且很简单。

XAML与上面相同。

WebInterface.cs:

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

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:

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