WPF載入HTML、WPF與JavaScript互動

來源:互聯網
上載者:User

標籤:xmlns   info   com   flags   input   service   jquer   資料   out   

目錄

一、WebBrowser載入遠程網頁

二、WebBrowser載入本地網頁,註:不可以載入本機樣式CSS和指令碼JS檔案

三、WebBrowser隱藏網頁的JavaScript錯誤

四、網頁屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵

五、WPF程式與網頁JavaScript互動

六、建立伺服器,提供資料介面、Script、CSS檔案

一、WebBrowser載入遠程網頁

wbrExam.Source = new Uri("http://cnblogs.com/sntetwt");

二、WebBrowser載入本地網頁,註:不可以載入本機樣式CSS和指令碼JS檔案

Uri uri = new Uri("pack://application:,,,/WPFSctipt;component/res/template.html", UriKind.Absolute);Stream source = Application.GetResourceStream(uri).Stream;wbrExam.NavigateToStream(source);

三、WebBrowser隱藏網頁的JavaScript錯誤

this.wbrExam.SuppressScriptErrors(true);

 

/// <summary>/// WebBrowser隱藏網頁的JavaScript錯誤/// </summary>public static class WebBrowserExtensions{    public static void SuppressScriptErrors(this WebBrowser webBrowser, bool hide)    {        FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);        if (fiComWebBrowser == null) return;        object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser);        if (objComWebBrowser == null) return;        objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });    }}

四、網頁屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵

<script type="text/javascript">//屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵     document.oncontextmenu = function(){event.returnValue=false;}//屏蔽滑鼠右鍵     window.onhelp = function(){return   false}   //屏蔽F1協助     document.onkeydown = function(){     if   ((window.event.altKey)&&                     ((window.event.keyCode==37) ||       //屏蔽   Alt+   方向鍵   ←                     (window.event.keyCode == 39))) {     //屏蔽   Alt+   方向鍵   →         event.returnValue = false;    return false;}     /* 註:這還不是真正地屏蔽Alt+方向鍵,                 因為Alt+方向鍵彈出警告框時,按住Alt鍵不放,                 用滑鼠點掉警告框,這種屏蔽方法就失效了。*/     if   ((event.keyCode==8) ||                                 //屏蔽退格刪除鍵                     (event.keyCode==116) ||                             //屏蔽   F5   重新整理鍵                     (event.ctrlKe && event.keyCode==82)) {              //Ctrl   +   R         event.keyCode=0;         event.returnValue=false;     }     if   (event.keyCode==122){event.keyCode=0;event.returnValue=false;}         //屏蔽F11     if   (event.ctrlKey && event.keyCode==78) event.returnValue=false;      //屏蔽Ctrl+n     if   (event.shiftKey && event.keyCode==121)event.returnValue=false;     //屏蔽shift+F10     if   (window.event.srcElement.tagName == "A" && window.event.shiftKey)           window.event.returnValue = false;                                   //屏蔽shift加滑鼠左鍵新開一網頁     if   ((window.event.altKey)&&(window.event.keyCode==115)){              //屏蔽Alt+F4         window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");         return   false;     }     }     </script>

五、WPF程式與網頁JavaScript互動

public void Message(string str){    MessageBox.Show(str);}/// <summary>/// WebBrowser與JavaScript互動/// </summary>[System.Runtime.InteropServices.ComVisible(true)]public class OprateBasic{    private MainWindow instance;    public OprateBasic(MainWindow instance)    {        this.instance = instance;    }    //提供給JS調用    public void HandleMessage(string p)    {        instance.Message(p);    }}//CS調用JSprivate void Button_Click(object sender, RoutedEventArgs e){    this.wbrExam.InvokeScript("invokeScript", new object[] { "CS調用JS" });}

  

JS調用CSwindow.external.HandleMessage("JS調用CS");

  

//提供給CS調用function invokeScript(args) {            alert(args);}

六、建立伺服器,提供資料介面、Script、CSS檔案

總結:因為載入HTML檔案的時候,HTML沒有路徑,所以載入不了JS和CSS等外部檔案

建立遠端站台提供資料介面和外部檔案

完整的CS程式如下

using System;using System.Collections.Generic;using System.Linq;using System.IO;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;using System.Reflection;namespace WPFSctipt{    /// <summary>    /// MainWindow.xaml 的互動邏輯    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void Window_ContentRendered(object sender, EventArgs e)        {                        //載入遠程網頁            //wbrExam.Source = new Uri("http://cnblogs.com/sntetwt");            Uri uri = new Uri("pack://application:,,,/WPFSctipt;component/res/template.html", UriKind.Absolute);            Stream source = Application.GetResourceStream(uri).Stream;            //WebBrowser隱藏網頁的JavaScript錯誤            this.wbrExam.SuppressScriptErrors(true);            //WebBrowser與JavaScript互動            this.wbrExam.ObjectForScripting = new OprateBasic(this);            //載入本地HTML檔案            wbrExam.NavigateToStream(source);        }        public void Message(string str)        {            MessageBox.Show(str);        }        /// <summary>        /// WebBrowser與JavaScript互動        /// </summary>        [System.Runtime.InteropServices.ComVisible(true)]        public class OprateBasic        {            private MainWindow instance;            public OprateBasic(MainWindow instance)            {                this.instance = instance;            }            //提供給JS調用            public void HandleMessage(string p)            {                instance.Message(p);            }        }        //CS調用JS        private void Button_Click(object sender, RoutedEventArgs e)        {            this.wbrExam.InvokeScript("invokeScript", new object[] { "CS調用JS" });        }    }    /// <summary>    /// WebBrowser隱藏網頁的JavaScript錯誤    /// </summary>    public static class WebBrowserExtensions    {        public static void SuppressScriptErrors(this WebBrowser webBrowser, bool hide)        {            FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);            if (fiComWebBrowser == null) return;            object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser);            if (objComWebBrowser == null) return;            objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });        }    }}

  

ASMX檔案代碼

<Window x:Class="WPFSctipt.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="WPF載入HTML、WPF與JavaScript互動" Height="600" Width="800" ContentRendered="Window_ContentRendered">    <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="500"></RowDefinition>            <RowDefinition Height="*"></RowDefinition>        </Grid.RowDefinitions>        <WebBrowser Grid.Row="0" Name="wbrExam" Height="500" VerticalAlignment="Top" Width="800"/>        <Button Grid.Row="1" Content="CS調用JS" VerticalAlignment="Top" Width="200" Height="40" Margin="10" Click="Button_Click" />    </Grid></Window>

  

HTML代碼

<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head>    <meta charset="utf-8" />    <meta http-equiv="X-UA-Compatible" content="IE=8" />     <title></title>    <style type="text/css">        body{background:red;}        .div{height:400px;}    </style>    <script type="text/javascript" src="http://common.cnblogs.com/script/jquery.js"></script><script type="text/javascript">//屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵     document.oncontextmenu = function(){event.returnValue=false;}//屏蔽滑鼠右鍵     window.onhelp = function(){return   false}   //屏蔽F1協助     document.onkeydown = function(){         if   ((window.event.altKey)&&                         ((window.event.keyCode==37) ||       //屏蔽   Alt+   方向鍵   ←                         (window.event.keyCode == 39))) {     //屏蔽   Alt+   方向鍵   →             event.returnValue = false;        return false;    }         /* 註:這還不是真正地屏蔽Alt+方向鍵,                 因為Alt+方向鍵彈出警告框時,按住Alt鍵不放,                 用滑鼠點掉警告框,這種屏蔽方法就失效了。*/         if   ((event.keyCode==8) ||                                 //屏蔽退格刪除鍵                         (event.keyCode==116) ||                             //屏蔽   F5   重新整理鍵                         (event.ctrlKe && event.keyCode==82)) {              //Ctrl   +   R             event.keyCode=0;             event.returnValue=false;         }         if   (event.keyCode==122){event.keyCode=0;event.returnValue=false;}         //屏蔽F11         if   (event.ctrlKey && event.keyCode==78) event.returnValue=false;      //屏蔽Ctrl+n         if   (event.shiftKey && event.keyCode==121)event.returnValue=false;     //屏蔽shift+F10         if   (window.event.srcElement.tagName == "A" && window.event.shiftKey)               window.event.returnValue = false;                                   //屏蔽shift加滑鼠左鍵新開一網頁         if   ((window.event.altKey)&&(window.event.keyCode==115)){              //屏蔽Alt+F4             window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");             return   false;         }     }     </script>    <script type="text/javascript">        $(function () {            $("div").text("JavaScript被執行");            window.external.HandleMessage("JS調用CS");        })        function invokeScript(args) {            alert(args);        }    </script></head><body>    <div class="div"></div></body></html>

  

WPF載入HTML、WPF與JavaScript互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.