WinForm下WebView2实现JS与C#交互Demo

上传者: u011883435 | 上传时间: 2025-12-17 16:34:51 | 文件大小: 11.47MB | 文件类型: RAR
在.NET Framework或.NET Core的Windows Forms(WinForm)应用程序中,常常需要集成Web视图以展示网页内容,并可能需要与网页中的JavaScript代码进行交互。微软提供了WebView2控件,它是Chromium内核的新一代Web视图,使得在WinForm应用中实现与现代Web技术的集成变得更加容易。本示例“WinForm下WebView2实现JS与C#交互Demo”就展示了如何在C#中使用WebView2控件来实现JavaScript与C#之间的双向通信。 确保已安装Microsoft Edge WebView2 SDK。可以通过NuGet包管理器安装`Microsoft.WebView2.WinForms`包。安装完成后,在WinForm项目中引入`Microsoft.WebView2.WinForms`命名空间,这样就可以使用WebView2控件。 接着,在WinForm设计器中添加一个WebView2控件到窗体上,并在代码中初始化它。初始化通常包括设置初始URL、加载完成后的回调以及设置用户数据目录,以便WebView2存储本地缓存和用户数据: ```csharp private Microsoft.WebView2.WinForms.WebView2 webView2; private async void Form1_Load(object sender, EventArgs e) { webView2 = new Microsoft.WebView2.WinForms.WebView2(); webView2.Dock = DockStyle.Fill; Controls.Add(webView2); // 设置初始URL webView2.Source = new Uri("https://example.com"); // 加载完成事件 webView2.CoreWebView2InitializationCompleted += (sender, args) => { // 设置用户数据目录 webView2.CoreWebView2.Environment = await Microsoft.WebView2.Core.CoreWebView2Environment.CreateAsync(null, null); }; } ``` 接下来,实现JS与C#的交互。通过`AddScriptToExecuteOnDocumentCreated`方法,可以在网页加载时执行一段JavaScript代码,这段代码可以注册一个全局函数,用于调用C#方法: ```csharp private void RegisterJsCallCSharp() { webView2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync( "window.callCSharp = function(param) { window.chrome.webview.postMessage(param); }"); } ``` 这里定义了一个`callCSharp`函数,当JavaScript需要调用C#方法时,可以调用这个函数并传递参数。 为了接收JavaScript的postMessage,我们需要监听`WebMessageReceived`事件,然后在事件处理程序中解析接收到的消息并调用相应的C#方法: ```csharp private async void WebView2_CoreWebView2/WebMessageReceived(object sender, Microsoft.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e) { string message = e.Message.ToString(); await webView2.CoreWebView2.ExecuteScriptAsync($"console.log('C# received: {message}');"); // 调用C#方法处理接收到的消息 ProcessMessage(message); } ``` 同时,C#也可以调用JavaScript代码。使用`ExecuteScriptAsync`方法可以执行任意JavaScript代码: ```csharp private async void CallJsFunctionFromCSharp(string functionName, params object[] args) { StringBuilder script = new StringBuilder(); script.Append(functionName); script.Append("("); for (int i = 0; i < args.Length; i++) { if (i > 0) script.Append(","); script.Append(JsonConvert.SerializeObject(args[i])); } script.Append(");"); await webView2.CoreWebView2.ExecuteScriptAsync(script.ToString()); } ``` 这个`CallJsFunctionFromCSharp`方法接受一个函数名和任意数量的参数,将其序列化为JSON字符串并拼接成JavaScript调用语句。 通过以上步骤,我们已经在WinForm应用中实现了WebView2控件的集成,让JavaScript可以调用C#方法,同时C#也能调用JavaScript函数。这在开发涉及网页和桌面应用交互的场景中非常有用,例如,你可以从网页触发桌面应用的功能,或者从桌面应用更新网页的内容。这个“WinForm下WebView2实现JS与C#交互Demo”提供了一个基础模板,你可以根据实际需求进行扩展和调整。

文件下载

资源详情

[{"title":"( 477 个子文件 11.47MB ) WinForm下WebView2实现JS与C#交互Demo","children":[{"title":"edgeSettings_2.0-48b11410dc937a1723bf4c5ad33ecdb286d8ec69544241bc373f753e64b396c1 <span style='color:#111;'> 127.38KB </span>","children":null,"spread":false},{"title":"05281bd700f228c8_0 <span style='color:#111;'> 3.08KB </span>","children":null,"spread":false},{"title":"0563c7e8bdaaae3f_0 <span style='color:#111;'> 238B </span>","children":null,"spread":false},{"title":"057c44c033bde0ba_0 <span style='color:#111;'> 259B </span>","children":null,"spread":false},{"title":"0b78745e16125cf3_0 <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"0ceb07016eca7d35_0 <span style='color:#111;'> 231B </span>","children":null,"spread":false},{"title":"0d79bb22e2d022ba_0 <span style='color:#111;'> 330.86KB </span>","children":null,"spread":false},{"title":"0ed900294f309949_0 <span style='color:#111;'> 275B </span>","children":null,"spread":false},{"title":"11a648546a996afb_0 <span style='color:#111;'> 9.65KB </span>","children":null,"spread":false},{"title":"125f3584d2a1e657_0 <span style='color:#111;'> 6.32KB </span>","children":null,"spread":false},{"title":"173527f19bcb61af_0 <span style='color:#111;'> 104.16KB </span>","children":null,"spread":false},{"title":"1f221b1fdf928d87_0 <span style='color:#111;'> 243B </span>","children":null,"spread":false},{"title":"1fae4f7248ae1c29_0 <span style='color:#111;'> 244B </span>","children":null,"spread":false},{"title":"webviewdemo.projects.v5.2 <span style='color:#111;'> 352.99KB </span>","children":null,"spread":false},{"title":"webviewdemo.metadata.v5.2 <span style='color:#111;'> 180.54KB </span>","children":null,"spread":false},{"title":"20cc8a3dd0021e88_0 <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"20f6faf4c05795f7_0 <span style='color:#111;'> 3.87KB </span>","children":null,"spread":false},{"title":"242947eeb139c4ef_0 <span style='color:#111;'> 1.95KB </span>","children":null,"spread":false},{"title":"24cf4dd3033e6086_0 <span style='color:#111;'> 1.60KB </span>","children":null,"spread":false},{"title":"2abb94558a4803e9_0 <span style='color:#111;'> 245B </span>","children":null,"spread":false},{"title":"2ae8e07e0e2eff1b_0 <span style='color:#111;'> 733B </span>","children":null,"spread":false},{"title":"2cc192eab08d4c30_0 <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"2cd3ad6c69b72bc2_0 <span style='color:#111;'> 3.87KB </span>","children":null,"spread":false},{"title":"2deba50cd2f47c1a_0 <span style='color:#111;'> 236B </span>","children":null,"spread":false},{"title":"31578bdbb0ede81e_0 <span style='color:#111;'> 258B </span>","children":null,"spread":false},{"title":"32d5263a7c694b0d_0 <span style='color:#111;'> 3.01KB </span>","children":null,"spread":false},{"title":"33de790ea7ff2ecb_0 <span style='color:#111;'> 253B </span>","children":null,"spread":false},{"title":"34808145447d8b1d_0 <span style='color:#111;'> 282B </span>","children":null,"spread":false},{"title":"34bcc8a34868e8c7_0 <span style='color:#111;'> 2.18KB </span>","children":null,"spread":false},{"title":"34d2f6bfd2c58901_0 <span style='color:#111;'> 254B </span>","children":null,"spread":false},{"title":"35829c3b2a281a91_0 <span style='color:#111;'> 12.19KB </span>","children":null,"spread":false},{"title":"37f71edd13fb6f4b_0 <span style='color:#111;'> 243B </span>","children":null,"spread":false},{"title":"392620d871fdbc1b_0 <span style='color:#111;'> 5.00KB </span>","children":null,"spread":false},{"title":"3a7679f3768aaaf0_0 <span style='color:#111;'> 251B </span>","children":null,"spread":false},{"title":"3b02eee15d344248_0 <span style='color:#111;'> 246B </span>","children":null,"spread":false},{"title":"3d9750b9152a9ee5_0 <span style='color:#111;'> 193B </span>","children":null,"spread":false},{"title":"3fa11b5bb56d0418_0 <span style='color:#111;'> 6.50KB </span>","children":null,"spread":false},{"title":"3fab835819572eb8_0 <span style='color:#111;'> 975B </span>","children":null,"spread":false},{"title":"42f9ffc1a97378a2_0 <span style='color:#111;'> 3.13KB </span>","children":null,"spread":false},{"title":"45a4104b4fd359ac_0 <span style='color:#111;'> 68.16KB </span>","children":null,"spread":false},{"title":"45eeec937dcc05e3_0 <span style='color:#111;'> 242B </span>","children":null,"spread":false},{"title":"463d1ca842c40a83_0 <span style='color:#111;'> 264B </span>","children":null,"spread":false},{"title":"463d3605e7b721ab_0 <span style='color:#111;'> 242B </span>","children":null,"spread":false},{"title":"472fb5f4a6e7a46f_0 <span style='color:#111;'> 1.64KB </span>","children":null,"spread":false},{"title":"4928c9cba14b2a59_0 <span style='color:#111;'> 182.45KB </span>","children":null,"spread":false},{"title":"4a7bf7fd32cda2c3_0 <span style='color:#111;'> 250B </span>","children":null,"spread":false},{"title":"4cbf959577669fa9_0 <span style='color:#111;'> 253B </span>","children":null,"spread":false},{"title":"4cff4d446f782383_0 <span style='color:#111;'> 232B </span>","children":null,"spread":false},{"title":"4db44319e148c338_0 <span style='color:#111;'> 2.20KB </span>","children":null,"spread":false},{"title":"5092a7e76f38899c_0 <span style='color:#111;'> 9.00KB </span>","children":null,"spread":false},{"title":"509618d4084f7e77_0 <span style='color:#111;'> 236B </span>","children":null,"spread":false},{"title":"519afb99b0c33f21_0 <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"52ca160fea97c58d_0 <span style='color:#111;'> 236B </span>","children":null,"spread":false},{"title":"53b283f655542d51_0 <span style='color:#111;'> 4.05KB </span>","children":null,"spread":false},{"title":"54a94833784d446e_0 <span style='color:#111;'> 275B </span>","children":null,"spread":false},{"title":"54dac72e69d21f97_0 <span style='color:#111;'> 282B </span>","children":null,"spread":false},{"title":"56b8dd9ca03f700e_0 <span style='color:#111;'> 212B </span>","children":null,"spread":false},{"title":"584a3bb5dc02e8b9_0 <span style='color:#111;'> 6.50KB </span>","children":null,"spread":false},{"title":"5b9c7689ac891393_0 <span style='color:#111;'> 224B </span>","children":null,"spread":false},{"title":"5e220fbd2bef815c_0 <span style='color:#111;'> 182.20KB </span>","children":null,"spread":false},{"title":"5fc5b2408d9779a6_0 <span style='color:#111;'> 263B </span>","children":null,"spread":false},{"title":"609019e41fcf0838_0 <span style='color:#111;'> 6.27KB </span>","children":null,"spread":false},{"title":"61b8f393168cfc06_0 <span style='color:#111;'> 8.56KB </span>","children":null,"spread":false},{"title":"622196ee501069ea_0 <span style='color:#111;'> 214B </span>","children":null,"spread":false},{"title":"625f2f10111a4cad_0 <span style='color:#111;'> 251B </span>","children":null,"spread":false},{"title":"628c139184976eb3_0 <span style='color:#111;'> 3.87KB </span>","children":null,"spread":false},{"title":"651c746af911078d_0 <span style='color:#111;'> 9.03KB </span>","children":null,"spread":false},{"title":"6733458907c773e1_0 <span style='color:#111;'> 240B </span>","children":null,"spread":false},{"title":"67843da901ff03d0_0 <span style='color:#111;'> 238B </span>","children":null,"spread":false},{"title":"68a64c5a70e3b617_0 <span style='color:#111;'> 344.84KB </span>","children":null,"spread":false},{"title":"69adc8f0e9053ea4_0 <span style='color:#111;'> 4.40KB </span>","children":null,"spread":false},{"title":"6b5157e8eef67de4_0 <span style='color:#111;'> 232B </span>","children":null,"spread":false},{"title":"6da2589d32f4140a_0 <span style='color:#111;'> 260B </span>","children":null,"spread":false},{"title":"6f0be153eb182c2d_0 <span style='color:#111;'> 9.03KB </span>","children":null,"spread":false},{"title":"6f604f81abb6f9e1_0 <span style='color:#111;'> 240B </span>","children":null,"spread":false},{"title":"6feb1fcfd1b0f224_0 <span style='color:#111;'> 975B </span>","children":null,"spread":false},{"title":"709f6c7551be45ba_0 <span style='color:#111;'> 240B </span>","children":null,"spread":false},{"title":"70eda0fd8a2def4e_0 <span style='color:#111;'> 243B </span>","children":null,"spread":false},{"title":"73fafd86d2da7858_0 <span style='color:#111;'> 240B </span>","children":null,"spread":false},{"title":"76ec904a92be67dd_0 <span style='color:#111;'> 4.03KB </span>","children":null,"spread":false},{"title":"77560865f123bbe2_0 <span style='color:#111;'> 6.27KB </span>","children":null,"spread":false},{"title":"79e5e2dc4a4e975d_0 <span style='color:#111;'> 10.46KB </span>","children":null,"spread":false},{"title":"7a713ce2932fe147_0 <span style='color:#111;'> 267B </span>","children":null,"spread":false},{"title":"7ba72afc41a1b843_0 <span style='color:#111;'> 258B </span>","children":null,"spread":false},{"title":"7d759ee6867eb1dc_0 <span style='color:#111;'> 733B </span>","children":null,"spread":false},{"title":"7d9b1fcac9f9a4e7_0 <span style='color:#111;'> 282B </span>","children":null,"spread":false},{"title":"7fbb780abc636efc_0 <span style='color:#111;'> 2.20KB </span>","children":null,"spread":false},{"title":"81495a8caf7a1a1f_0 <span style='color:#111;'> 252B </span>","children":null,"spread":false},{"title":"87c3dc04dbf4195b_0 <span style='color:#111;'> 233B </span>","children":null,"spread":false},{"title":"8a223c356b30ae3c_0 <span style='color:#111;'> 239B </span>","children":null,"spread":false},{"title":"8a6160c22c76143c_0 <span style='color:#111;'> 275B </span>","children":null,"spread":false},{"title":"8a628226c35bd73d_0 <span style='color:#111;'> 223B </span>","children":null,"spread":false},{"title":"8f6ff4229e15fff2_0 <span style='color:#111;'> 9.00KB </span>","children":null,"spread":false},{"title":"934d6754abe4409b_0 <span style='color:#111;'> 1.64KB </span>","children":null,"spread":false},{"title":"936ea0c6425cdfae_0 <span style='color:#111;'> 4.78KB </span>","children":null,"spread":false},{"title":"9b7157270ea55c86_0 <span style='color:#111;'> 231B </span>","children":null,"spread":false},{"title":"9c0b6d0d842ab40f_0 <span style='color:#111;'> 2.95KB </span>","children":null,"spread":false},{"title":"9e5187c1a8af9c12_0 <span style='color:#111;'> 2.64KB </span>","children":null,"spread":false},{"title":"a183fd5138b1e7dd_0 <span style='color:#111;'> 2.64KB </span>","children":null,"spread":false},{"title":"a2ab8cf1bfb2894e_0 <span style='color:#111;'> 232B </span>","children":null,"spread":false},{"title":"......","children":null,"spread":false},{"title":"<span style='color:steelblue;'>文件过多,未全部展示</span>","children":null,"spread":false}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明