阅读 57

WPF 实现 Chrome 内核的 Webbrowser

1. 使用 Nuget 添加 cefsharp.wpf 库

2. 窗口中使用

1
2
3
xmlns:chrome="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
 
"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
{
    ///
    /// 提供给 Javascript 的对象
    ///
    public static string ExportedJavascriptOjbectName = "ntst";
 
    private ReportWindow _reportWindow = null;
    public NtstJSObject(ReportWindow reportWindow)
    {
        this._reportWindow = reportWindow;
    }
 
    ///
    /// 提供给 Javascript 调用的 closeWindow 接口
    ///
    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
///
/// Javascript 接口:closeWindow
///
///
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()

原文:https://www.cnblogs.com/simadi/p/15237831.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐