.NET中WebBrowser控件內部頁面的JS代碼與外部C#代碼的相互調用

原文地址:https://my.oschina.net/Tsybius2014/blog/643909

場景1:C#程序調用JS函數刷新網頁,輸出再見兩字;測試目標:C#調用JS函數

場景2:C#程序調用JS函數刷新網頁,輸出文字爲用戶輸入的文字;測試目標:C#調用帶參數的JS函數

場景3:C#程序調用JS函數獲取今日的年月日信息(yyyy-MM-dd);測試目標:C#能否正確接收JS函數返回值

場景4:JS調用C#函數,輸出上面↑↑↑(指bulletin)的文字內容;測試目標:JS調用C#應用程序中帶參數的函數

場景5:JS調用C#函數,將左側輸入框中的內容轉大寫後放到右側輸入框中;測試目標:JS調用C#應用程序中帶參數的函數並接收返回值

winform程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WebBrowserJsTest
{
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        } 

        /// <summary>Load函數</summary>         
        ///<param name="sender"></param>         
        ///<param name="e"></param>         
        private void FormMain_Load(object sender, EventArgs e)
        {
            try {
                string path = Environment.CurrentDirectory + "\\WebBrowserJsTest.html";
                                webBrowser.Navigate(path);
                webBrowser.ObjectForScripting = this;
            } catch(Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        } 

        /// <summary>測試1</summary>         
        /// /// <param name="sender"></param>         
        /// /// <param name="e"></param>         
        private void btn4Test1_Click(object sender, EventArgs e)
        {
            webBrowser.Document.InvokeScript("sayGoodBye", null);
        } 

        /// <summary>測試2</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>         
        private void btn4Test2_Click(object sender, EventArgs e)
        {
            webBrowser.Document.InvokeScript("changeBulletin", new object[] {
                txt4Test2.Text
            });
        } 

        /// <summary>測試3</summary>
        /// <param name="sender"></param> 
        /// <param name="e"></param>         
        private void btn4Test3_Click(object sender, EventArgs e)
        {
            object obj = webBrowser.Document.InvokeScript("getTodaysDate", null);
            MessageBox.Show(obj.ToString());
        } 

        /// <summary>測試4</summary>
        /// <param name="word"></param>         
        public void ShowBulletin(string word)
        {
            MessageBox.Show(word);
        } 

        /// <summary>測試5</summary>
        /// <param name="word"></param>
        /// <returns></returns>         
        public string ToUpper(string word)
        {
            return word.ToUpper();
        }
    }
}

html代碼

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head> 
  <meta charset="utf-8" /> 
  <title></title> 
 </head> 
 <body>
   這是一個測試用的頁面 
  <hr /> ========輸出都寫在這裏======== 
  <div id="bulletin">
   你好
  </div> 
  <hr /> 測試4:JS調用C#函數,輸出上面↑↑↑的文字內容
  <br /> 測試目標:JS調用C#應用程序中帶參數的函數
  <br /> 
  <input type="button" id="showBulletin" value="調用C#1" onclick="showBulletin();" /> 
  <hr /> 測試5:JS調用C#函數,將左側輸入框中的內容轉大寫後放到右側輸入框中
  <br /> 測試目標:JS調用C#應用程序中帶參數的函數並接收返回值
  <br /> 
  <input type="text" id="inputValue" /> 
  <input type="button" id="toUpper" value="調用C#2" onclick="toUpper();" /> 
  <input type="text" id="returnValue" /> 
  <script>
      //測試1
      function sayGoodBye() {
        document.getElementById("bulletin").innerHTML = "再見";
      }
      //測試2
      function changeBulletin(word) {
        document.getElementById("bulletin").innerHTML = word;
      }
      //測試3
      function prefixInteger(num, n) {
        return (Array(n).join(0) + num).slice(-n);
      }
      function getTodaysDate() {
        var dateNow = new Date();
        var year = dateNow.getFullYear();
        var month = (dateNow.getMonth() + 1);
        var day = dateNow.getDate();
        return year + "-" + prefixInteger(month, 2) + "-" + prefixInteger(day, 2);
      }
      //測試4
      function showBulletin() {
        var word = document.getElementById("bulletin").innerHTML;
        window.external.ShowBulletin(word);
      }
      //測試5
      function toUpper() {
        var word = document.getElementById("inputValue").value;
        var ret = window.external.ToUpper(word);
        document.getElementById("returnValue").value = ret;
      }
    </script>  
 </body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章