WPF 实现 Chrome 内核的 Webbrowser
1. 使用 Nuget 添加 cefsharp.wpf 库
2. 窗口中使用
1
2
3
|
xmlns:chrome= "clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" <chrome:ChromiumWebBrowser x:Name= "wbrReport" /> |
3. 打开指定的 URL
1
2
|
// 加载 URL this .wbrReport.Address = "www.baidu.com" ; |
4. Cefsharp.wpf 给 javascript 提供接口
1. 设置外部调用的对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class NtstJSObject { /// <summary> /// 提供给 Javascript 的对象 /// </summary> public static string ExportedJavascriptOjbectName = "ntst" ; private ReportWindow _reportWindow = null ; public NtstJSObject(ReportWindow reportWindow) { this ._reportWindow = reportWindow; } /// <summary> /// 提供给 Javascript 调用的 closeWindow 接口 /// </summary> public void closeWindow() { if ( this ._reportWindow != null ) { this ._reportWindow.CloseWindow( this ); } } } |
2. 窗体构造函数中绑定 js 对象
1
2
3
4
|
// 绑定 Javascript 调用对象 CefSharpSettings.WcfEnabled = true ; this .wbrReport.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true ; this .wbrReport.JavascriptObjectRepository.Register(NtstJSObject.ExportedJavascriptOjbectName, new NtstJSObject( this ), false , BindingOptions.DefaultBinder); |
3. 窗口提供真正调用接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/// <summary> /// Javascript 接口:closeWindow /// </summary> /// <param name="ntstJSObject"></param> public void CloseWindow(NtstJSObject ntstJSObject) { if ( this ._logger != null ) { this ._logger.WriteInformation( string .Format( "Javascript call to: {0}" , nameof(CloseWindow))); } this .Dispatcher.Invoke( new Action(() => { this .Close(); })); } |
4. Javascript 调用方式
1
|
ntst.closeWindow() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2017-09-07 上传指定url文件到阿里云oss