WPF loading HTML, WPF interacting with JavaScript

Source: Internet
Author: User
Tags tagname

Directory

WebBrowser loading a remote Web page

Second, WebBrowser loading local page, note: Can not load local style CSS and script js file

Third, WebBrowser JavaScript error to hide Web pages

Four, the right mouse button screen, CTRL + N, SHIFT+F10, F11, F5 refresh, backspace key

V. WPF programs interacting with Web page javascript

VI. Create server, provide data interface, Script, CSS file

WebBrowser loading a remote Web page

Wbrexam.source = new Uri ("HTTP://CNBLOGS.COM/SNTETWT");

Second, WebBrowser loading local page, note: Can not load local style CSS and script js file

Uri uri = new Uri ("Pack://application:,,,/wpfsctipt;component/res/template.html", urikind.absolute); Stream Source = Application.getresourcestream (URI). Stream;wbrexam.navigatetostream (source);

Third, WebBrowser JavaScript error to hide Web pages

This.wbrExam.SuppressScriptErrors (TRUE);

<summary>///WebBrowser JavaScript error for hidden Web page//</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});}    

Four, the right mouse button screen, CTRL + N, SHIFT+F10, F11, F5 refresh, backspace key

<script type= "Text/javascript" >//shield right mouse button, CTRL + N, SHIFT+F10, F11, F5 refresh, backspace document.oncontextmenu = function () { Event.returnvalue=false;} Shield right mouse button window.onhelp = function () {return false}//Mask F1 help Document.onkeydown = function () {if (window.       Event.altkey) && ((window.event.keycode==37) | | Shield ALT + arrow key ← (Window.event.keyCode = = 39))) {//shield ALT + arrow key →EVENT.RETURNV    Alue = false;     return false;} /* Note: This is not really shielded ALT + arrow key, because ALT + arrow key pop-up warning box, ALT key, click off the warning box with the mouse, this shielding method is invalid.                                 */if ((event.keycode==8) | | |                             Mask backspace Delete key (event.keycode==116) | | Mask F5 Refresh key (Event.ctrlke && event.keycode==82)) {//ctrl + R Eve         nt.keycode=0;     Event.returnvalue=false;  } if (event.keycode==122) {event.keycode=0;event.returnvalue=false;}       Shielded F11 if (Event.ctrlkey && event.keycode==78) Event.returnvalue=false;     Shielded CTRL + N if (event.shiftkey && event.keycode==121) Event.returnvalue=false; Shielded Shift+f10 if (window.event.srcElement.tagName = = "A" && Window.event.shiftKey) window.event.re                                   Turnvalue = false;         Block shift plus left mouse button new page if ((Window.event.altKey) && (window.event.keycode==115)) {//Mask ALT+F4         Window.showmodelessdialog ("About:blank", "" "," dialogwidth:1px;dialogheight:1px ");     return false; }} </script>

V. WPF programs interacting with Web page javascript

public void Message (String str) {    MessageBox.Show (str);} <summary>///WebBrowser interacts with JavaScript//</summary>[system.runtime.interopservices.comvisible (True )]public class opratebasic{    private MainWindow instance;    Public Opratebasic (MainWindow instance)    {        this.instance = instance;    }    Provided to JS call public    void Handlemessage (string p)    {        instance. Message (P);    }} CS calls jsprivate void Button_Click (object sender, RoutedEventArgs e) {    this.wbrExam.InvokeScript ("Invokescript", New object[] {"CS call js"});

  

JS call CSwindow.external.HandleMessage ("JS call cs");

  

Provided to CS call function Invokescript (args) {            alert (args);}

VI. Create server, provide data interface, Script, CSS file

Summary: Because the HTML file is loaded, HTML does not have a path, so can not load JS and CSS and other external files

Create a remote site to provide data interfaces and external files

The complete CS program is as follows

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 interactive logic///</summary> public partial class Mainwindow:window {p        Ublic MainWindow () {InitializeComponent ();            private void Window_contentrendered (object sender, EventArgs e) {//Load Remote Web page            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 Error This.wbrExam.SuppressScriptErrors (true) for hidden pages;            WebBrowser interaction with JavaScript this.wbrExam.ObjectForScripting = new Opratebasic (this);        Load the local HTML file wbrexam.navigatetostream (source);        } public void Message (String str) {MessageBox.Show (str); }//<summary>//WebBrowser interacting with JavaScript///</summary> [System.Runtime.Interop            Services.comvisible (true)] public class Opratebasic {private MainWindow instance;            Public Opratebasic (MainWindow instance) {this.instance = instance; }//provided to JS call public void Handlemessage (string p) {instance.            Message (P); }}//cs call js private void Button_Click (object sender, RoutedEventArgs e) {THIS.WBR Exam.invokescript ("Invokescript", New ObjeCt[] {"CS call js"}); }}///<summary>//WebBrowser JavaScript error for hidden pages///</summary> public static class WebBrowser            Extensions {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 file code

<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 loading HTML, WPF interacting with JavaScript" height= "width=" contentrendered= "window_contentrendered" >    < grid>        <Grid.RowDefinitions>            <rowdefinition height= "$" ></RowDefinition>            < RowDefinition height= "*" ></RowDefinition>        </Grid.RowDefinitions>        <webbrowser grid.row= "0" name= "wbrexam" height= "verticalalignment=" Top "width=" "/> <button"        "1" grid.row= "CS Call JS "Verticalalignment=" Top "width=" "height=" "margin=" "click=" Button_Click "/>    </grid></" Window>

  

HTML code

<! DOCTYPE html>

  

WPF loading HTML, WPF interacting with JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.