javascript调用外部wpf的方法

今天因为项目需要在wpf嵌入web页面,然后在web页面调用wpf的方法,于是突击学习了一下,记下备忘。^_^

1、wpf中新建一个类WpfForScriptingHelper

 1 namespace wpf_web
 2 {
 3     [System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为可访问com
 4     public class WpfForScriptingHelper
 5     {
 6         //wpf的窗口类
 7         MainWindow mainWindow;
 8         public WpfForScriptingHelper(MainWindow main)
 9         {
10             mainWindow = main;
11         }
12         //这个方法就是网页上要访问的方法,js通过window.external.HtmlCmd('我是javascript请求过来的!')
13         public void HtmlCmd(string cmd)
14         {
15             MessageBox.Show(cmd);
16         }
17     }
18 }
创建一个类

 

2、在wpf窗口MainWindow中的构造函数中加入以下代码,也可以在该窗口的其他合适地方

            WpfForScriptingHelper helper = new WpfForScriptingHelper(this);
            this.myWebBrowser.ObjectForScripting = helper;

3、html页面代码。

 1 <html xmlns="http://www.w3.org/1999/xhtml"> 
 2 
 3 <head runat="server"> 
 4 
 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    
 6 
 7  <title></title>
 8 
 9 </head> 
10 
11 <body>     
12 
13 <form id="form1" runat="server">    
14 
15  <div>        
16 
17  <input type="button" id="mybutton" value="点击我吧!" οnclick="window.external.HtmlCmd('我是javascript请求过来的!')" />    
18 
19  </div></form>
20 </body>
21 
22 </html>
html代码

 

转载于:https://www.cnblogs.com/simpleZone/p/3402338.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值