弹来弹去跑马灯!

WPF webbowser 交互

//

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"


    xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

        <wfi:WindowsFormsHost x:Name="c_wfh" Margin="0,90,0,0">
            <winform:WebBrowser ScriptErrorsSuppressed="True"  x:Name="web">
            </winform:WebBrowser>
        </wfi:WindowsFormsHost>
        <Button Content="Button" HorizontalAlignment="Left" Margin="34,10,0,0" VerticalAlignment="Top" Width="120" Height="75" Click="Button_Click"/>
    </Grid>
</Window>

  

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            web.ObjectForScripting = new OprateBasic();
            Loaded += MainWindow_Loaded;
            web.Navigate("https://www.runoob.com/w3cnote/javascript-settimeout-usage.html");
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            WebbowserHelper.SetWebBrowserFeatures(11);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            //  web.Document.InvokeScript("eval", new string[] { "function tt(){ window.external.NotifyMsg(new Date()); } ;setTimeout('tt', 3000);" }); 
            // web.Document.InvokeScript("eval", new string[] { "if(document.getElementsByClassName('fff').length>0){window.external.NotifyMsg(66)} else{window.external.NotifyMsg(0)};" });
            //下面这种也可以返回数据
            var i = web.Document.InvokeScript("eval", new string[] { "if(document.getElementsByClassName('fff').length>0){  1;} else{  0;}" });
            Title = "" + i;

        }

    }

    [System.Runtime.InteropServices.ComVisible(true)]
    public class OprateBasic
    {
        public void NotifyMsg(string msg)
        {
            MessageBox.Show(msg);
        }
    }
}

  

posted @ 2020-08-11 16:44  wgscd  阅读(308)  评论(0编辑  收藏  举报