Examples of C # Windows program application interaction with JavaScript programs

Source: Internet
Author: User

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 app

Code: 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 method

JavaScript 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

Related Article

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.