WPF和WebBrowser JS交互

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WebKit;
using System.Net;
using System.Xml;
namespace ZhenaiTool
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
           InitializeComponent();
           OprateBasic OBasic=new OprateBasic(this);
           web.ObjectForScripting = OBasic;
            web.Url = new Uri("http://www.baidu.com");
           Loaded += new RoutedEventHandler(Window2_Loaded);
        }

        public void palymusic(string msg)
        {
            MessageBox.Show(msg,"---------title-------",MessageBoxButton.OKCancel);
        }
       private void button1_Click(object sender, RoutedEventArgs e)
        {
          web.Document.InvokeScript("eval", new string[] { "alert('test msg from web');  window.external.palymusic(777777)" });

        }

        void Window2_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }



    [System.Runtime.InteropServices.ComVisible(true)] // 将该类设置为com可访问
    public class OprateBasic
    {

        private Window2 instance;
        public OprateBasic(Window2 instance)
        {
            this.instance = instance;
        }

        public void palymusic(string msg)
        {
           instance.palymusic(msg);
        }


    }


}

  

猜你喜欢

转载自www.cnblogs.com/wgscd/p/9342264.html