首页 技术 正文
技术 2022年11月13日
0 收藏 399 点赞 4,887 浏览 2730 个字

 

有时我们在winform项目中嵌入了网页,想通过html页面调用后台方法,如何实现呢?其实很简单,主要有三部:

 

1、在被调用方法类上加上[ComVisible(true)]标签,意思就是当前类可以com组件的形式供外包调用

2、在webBrowser控件中设置可被html页面调用的类即:webBrowser1.ObjectForScripting = this;前端即可通过window.external访问this对象

3、html页面调用后台方法:window.external.方法名(); 此处的window.external相当于webBrowser1.ObjectForScripting

 

一、后台代码:

[csharp]
view plain
copy
print?

  1. namespace jsInWebBrowserCallCSharpMethod  
  2. {  
  3.     [ComVisible(true)] //1、必须设置且为true,否则设置webBrowser1.ObjectForScripting对象时会报错  
  4.     public partial class Form1 : Form  
  5.     {  
  6.         public Form1()  
  7.         {  
  8.             InitializeComponent();  
  9.             webBrowser1.Url = new Uri(Application.StartupPath + “\\htmls\\test.html”);  
  10.             webBrowser1.ObjectForScripting = this;//2、设置js中window.external对象代表的类  
  11.         }  
  12.         /// <summary>  
  13.         /// 供webBrowser页面中js调用的方法  
  14.         /// </summary>  
  15.         /// <param name=”mess”></param>  
  16.         public void ShowMessage(string mess)  
  17.         {  
  18.             MessageBox.Show(mess);  
  19.         }  
  20.   
  21.     }  
  22. }  

namespace jsInWebBrowserCallCSharpMethod

{

[ComVisible(true)] //1、必须设置且为true,否则设置webBrowser1.ObjectForScripting对象时会报错

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

webBrowser1.Url = new Uri(Application.StartupPath + “\\htmls\\test.html”);

webBrowser1.ObjectForScripting = this;//2、设置js中window.external对象代表的类

}

/// <summary>

/// 供webBrowser页面中js调用的方法

/// </summary>

/// <param name=”mess”></param>

public void ShowMessage(string mess)

{

MessageBox.Show(mess);

}

 

}

}

 

二、前端test.html代码:

[csharp]
view plain
copy
print?

  1. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>  
  2. <html xmlns=”http://www.w3.org/1999/xhtml”>  
  3. <head>  
  4.     <title>测试调用winform后台方法页面</title>  
  5.       
  6.     <script type=”text/javascript”>  
  7.           
  8.        window.onload=function(){  
  9.   
  10. var btn=document.getElementById(‘btnCallCSharpMethod’);  
  11. btn.onclick=function(){  
  12.   
  13. window.external.ShowMessage(‘成功调用winform类中的方法!’);//3、此处window.external相当于winform中设置的webBrowser.ObjectForScripting对象  
  14.   
  15. }  
  16.   
  17. }  
  18.     </script>  
  19.   
  20. </head>  
  21. <body style=’text-align:center;’>  
  22.    <input type=’button’ id=’btnCallCSharpMethod’ value=’调用winform类中的方法’ />  
  23. </body>  
  24. </html>  

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>测试调用winform后台方法页面</title>

 

<script type=”text/javascript”>

 

window.onload=function(){

 

var btn=document.getElementById(‘btnCallCSharpMethod’);

btn.onclick=function(){

 

window.external.ShowMessage(‘成功调用winform类中的方法!’);//3、此处window.external相当于winform中设置的webBrowser.ObjectForScripting对象

 

}

 

}

</script>

 

</head>

<body style=’text-align:center;’>

<input type=’button’ id=’btnCallCSharpMethod’ value=’调用winform类中的方法’ />

</body>

</html>

 

按照上面1,2,3点操作,就能实现html页面调用winform 后台方法了。源码点击打开链接下载。

 

来自: http://blog.csdn.net/taoerchun/article/details/49782739

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848