C # Windows program application and JavaScript program interaction Implementation Example one, the establishment of Web page code (including JS method code and access to external Windows application events)
It is important to note that JS accesses external Windows application methods and needs to external the Windows object
Example: Window.external.CSharpfunction (XX,XX,XX);
1<! DOCTYPE html>2 3"en"xmlns="http://www.w3.org/1999/xhtml">45<meta http-equiv="Content-language"Content="ZH-CN">6<script language="JavaScript"Type="Text/javascript">7<!--provides a way for C # program calls--8 function MessageBox (message)9 {Ten alert (message); One } A</script> - - the<body> -<!--call C # Method-- -<button onclick="Window.external.MyMessageBox (' JavaScript access C # code ')"> - JavaScript access to C # code +</button> -</body> +Ii. Create a c#windows form appCode: Note that you need to add an accessibility setting for COM to the Form1 class [System.Runtime.InteropServices.ComVisible (True)]1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceWinformjsdemo One { A //set COM to be externally accessible -[System.Runtime.InteropServices.ComVisible (true)] - Public Partial classForm1:form the { - PublicForm1 () - { - InitializeComponent (); +System.IO.FileInfo file =NewSystem.IO.FileInfo ("javascript//index.html"); - + //the page path displayed by the WebBrowser control AWebbrowser1.url =NewUri (file. FullName); at - //To set the current class to be accessible by script -Webbrowser1.objectforscripting = This; - } - - in //methods that are called by external JS - Public voidMymessagebox (stringmessage) to { + - MessageBox.Show (message); the } * $ Private voidButton1_Click (Objectsender, EventArgs e)Panax Notoginseng { - //Call the JavaScript MessageBox method and pass in the parameters the Object[] objects =New Object[1]; + Aobjects[0] ="C # access JavaScript scripts"; the +WebBrowser1.Document.InvokeScript ("MessageBox", objects); - } $ } $}
Run Result: C # calls the JavaScript methodJavaScript calls the C # method:Reference: http://www.cnblogs.com/xds/archive/2007/03/02/661838.html
Examples of C # Windows program application interaction with JavaScript programs