zoukankan      html  css  js  c++  java
  • WPF 实现 Chrome 内核的 Webbrowser

    1. 使用 Nuget 添加 cefsharp.wpf 库

    2. 窗口中使用

    xmlns:chrome="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
    
    <chrome:ChromiumWebBrowser x:Name="wbrReport"/>
    

    3. 打开指定的 URL

    // 加载 URL
    this.wbrReport.Address = "www.baidu.com";
    

    4. Cefsharp.wpf 给 javascript 提供接口

      1. 设置外部调用的对象

    	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 对象

    	// 绑定 Javascript 调用对象
    	CefSharpSettings.WcfEnabled = true;
    	this.wbrReport.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
    	this.wbrReport.JavascriptObjectRepository.Register(NtstJSObject.ExportedJavascriptOjbectName, new NtstJSObject(this), false, BindingOptions.DefaultBinder);
    

      3. 窗口提供真正调用接口

    	/// <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 调用方式

    ntst.closeWindow()
    

      

      

  • 相关阅读:
    进程&多道技术
    linux 三大利器 grep sed awk sed
    linux 三大利器 grep sed awk 正则表达式
    cmd pyhton
    ConfigParser 模块
    几种数据类型的简单概念
    python基础关于字符串的常用操作
    寻租行为
    集约式发展 与 粗放式发展
    最小二乘法
  • 原文地址:https://www.cnblogs.com/462079558/p/14480273.html
Copyright © 2011-2022 走看看