在CefSharp中使用本地构建的网页(Working with locally built web page in CefSharp)

我在我的Winform中创建了一个CefSharp浏览器,我需要在内存中动态构建一个HTML页面,然后让CefSharp呈现它。

理想情况下,我想传递构造函数一个带有HTML的字符串,但它期望一个URL。 答案可能是否定的,但是有没有一个指令可以让字符串预先让CefSharp知道它是一个包含网页的字符串? 那么CefSharp会创建一个临时文件?

如果不是,Chromium temp文件夹的设置在哪里? 如果我向那里写入文件并将其作为完全合格的路径传递,它会起作用吗? 我知道Chrome会支持像file:///Users/dmacdonald/Documents/myFile.htm这样的URL,但不知道如何使用temp结构来构建URL。

这是我的新代码,但我的浏览器对象没有ResourceHandler属性。 我看到它有一个ResourceHandlerFactory

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using CefSharp.WinForms;

using CefSharp;

namespace DanCefWinForm

{

public partial class Form1 : Form

{

public const string TestResourceUrl = "http://maps/resource/load";

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

ChromiumWebBrowser browser = new ChromiumWebBrowser("http://maps/resource/load")

{

Dock = DockStyle.Fill,

};

var handler = browser.ResourceHandler;

browser.Location = new Point(20, 20);

browser.Size = new Size(100, 100);

this.Controls.Add(browser);

}

}

}

I have a CefSharp browser created in my Winform and I need to dynamically build an HTML page in memory and then have CefSharp render it.

Ideally I would like to pass the constructor a string with the HTML in it but it is expecting a URL. The answer is probably no, but is there a directive you can prepend the string with to let CefSharp know it is a string that contains a web page? Then CefSharp will create a temp file?

If not, where is the Chromium temp folder set to? Will it work if I write a file to there and then pass that as a fully qualified path? I know Chrome will support something like file:///Users/dmacdonald/Documents/myFile.htm as a URL but not sure how to form a URL if using the temp structure.

Here is my new code but my browser object doesn't have a ResourceHandler property. I see it has a ResourceHandlerFactory

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using CefSharp.WinForms;

using CefSharp;

namespace DanCefWinForm

{

public partial class Form1 : Form

{

public const string TestResourceUrl = "http://maps/resource/load";

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

ChromiumWebBrowser browser = new ChromiumWebBrowser("http://maps/resource/load")

{

Dock = DockStyle.Fill,

};

var handler = browser.ResourceHandler;

browser.Location = new Point(20, 20);

browser.Size = new Size(100, 100);

this.Controls.Add(browser);

}

}

}

原文:https://stackoverflow.com/questions/28697613

更新时间:2019-11-03 11:25

最满意答案

简单的方法(一个“文件”,一页)

LoadString()可用于直接从字符串中加载:

ChromiumWebBrowser.LoadString(string html, string url);

或者, LoadHtml()可以从给定编码中的字符串中加载:

ChromiumWebBrowser.LoadHtml(string html, string url, Encoding encoding);

我尝试了两种方法,他们似乎都能工作,至少在CefSharp.Wpf v51.0.0中 。 根据WebBrowserExtensions.cs , LoadHtml()使用RegisterHandler()来注册一个ResourceHandler 。 我不清楚LoadString()是如何工作的,但是这两个函数似乎都有相同的效果。

请确保为假的URL使用有效的URL格式,例如:

https://myfakeurl.com

复杂的方法(多个“文件”,如doc +图像)

创建一个从IResourceHandlerFactory派生的类。 使用VS2015,将鼠标悬停在带红色下划线的名称上应该提供实现界面的选项。 这个自动完成选项大大简化了类的创建,所以一定要使用它。

与第1步类似,创建一个从IResourceHandler派生的类。 如果可以,请务必使用实现界面自动完成选项。

在第1步中创建的类(从IResourceHandlerFactory派生)中,有一个名为GetResourceHandler()的函数。 在这个函数中,从步骤2返回一个派生类的新实例(基于IResourceHandler )。 由于Web浏览器可能会同时请求多个文件,因此在此处使用new内容至关重要 每个IResourceHandler实例都应该处理来自浏览器的一个请求(不用担心,这是为你完成的)。

正如OP所述,浏览器控件有一个名为ResourceHandlerFactory的成员。 将此成员设置为等于您在步骤1中创建的类的新实例(派生自IResourceHandlerFactory )。 这是将Chromium Web浏览器控件链接到您的接口类的。 在第3步中,您链接了两个类,所以我们有一个完整的链。

在步骤2的类中,有一个名为ProcessRequest()的函数。 这是网页发出请求时调用的第一个函数。 您的目标是记录请求的URL和任何POST数据,然后决定是否允许请求,调用callback.Continue()或callback.Cancel() 。 返回true继续。

再次来自第2步的类中,有一个名为GetResponseHeaders()的函数。 这是所谓的第二个功能。 您的目标是检查URL,可能从您存储它的位置(但尚未发送它)获取文件数据,确定响应长度(文件或字符串大小),并在响应对象中设置适当的状态码。 一定要设置所有这些变量,以便请求可以正确进行。

最后一步,再次从第2步开始,在第三个调用的函数中完成请求: ReadResponse() 。 在此函数中,将您在步骤6中获取的数据写入dataOut流。 如果您的数据超过32kB,您可能需要将其发送到多个区块。 请务必将给定调用中写入的数量限制为dataOut流的长度。 将bytesRead设置为您在此特定调用中编写的任何内容。 在最后一次调用中,当不再有数据时,只需将bytesRead设置为零并返回false 。 因为给定文件可能需要多次调用,所以一定要跟踪当前的读取位置,以便知道自己在哪里以及发送了多少数据。

对于那些不熟悉此事的人,可以将直接编译到EXE中的数据文件添加到项目中,并将其“Build Action”设置为“Embedded Resource”,然后使用System.Reflection.Assembly.GetManifestResourceStream()编程方式加载它们的数据System.Reflection.Assembly.GetManifestResourceStream() 。 使用上述方法, 不需要从磁盘创建或读取任何文件 。

The Simple Approach (one "file", one page)

LoadString() can be used to load directly from a string:

ChromiumWebBrowser.LoadString(string html, string url);

Alternatively, LoadHtml() can load from a string in a given encoding:

ChromiumWebBrowser.LoadHtml(string html, string url, Encoding encoding);

I tried both, and they both seem to work, at least with CefSharp.Wpf v51.0.0. According to WebBrowserExtensions.cs, LoadHtml() uses RegisterHandler() to register a ResourceHandler. It is not clear to me how LoadString() works, but both functions seem to have the same effect.

Be sure to use a valid URL format for the fake URL, such as:

https://myfakeurl.com

The Complex Approach (multiple "files", such as doc + images)

Create a class deriving from IResourceHandlerFactory. Using VS2015, mousing over the red-underlined name should give the option of Implement interface. This auto-complete option vastly simplifies creation of the class, so be sure to use it.

Similar to in step 1, create a class deriving from IResourceHandler. Be sure to use the Implement interface auto-complete option if you can.

In the class created in step 1 (derived from IResourceHandlerFactory), there is a function called GetResourceHandler(). Within this function, return a new instance of your derived class from step 2 (based on IResourceHandler). Using new here is essential since the Web browser may request multiple files simultaneously. Each IResourceHandler instance should handle one request from the browser (no worries, this is done for you).

As mentioned by OP, the browser control has a member called ResourceHandlerFactory. Set this member equal to a new instance of your class you created in step 1 (deriving from IResourceHandlerFactory). This is what links the Chromium Web Browser control to your interface classes. In step 3 you linked both your classes, so we have a full chain.

Within the class from step 2, there is a function called ProcessRequest(). This is the first function called when a request is made by a Web page. Your goal here is to record the requested URL and any POST data, then decide whether to allow the request, calling either callback.Continue() or callback.Cancel(). Return true to continue.

Again in the class from step 2, there is a function called GetResponseHeaders(). This is the second function called. Your goal here is to check the URL, possibly fetching file data from wherever you store it (but not yet sending it), determine the response length (file or string size), and set an appropriate status code within the response object. Be sure to set all these variables so the request can proceed correctly.

Your final step, again in the class from step 2, is to complete the request within the third called function: ReadResponse(). Within this function, write your data fetched in step 6 to the dataOut stream. If your data exceeds about 32kB, you may need to send it in multiple chunks. Be absolutely sure to limit the amount you write in a given call to the length of the dataOut stream. Set bytesRead to whatever you wrote in this particular call. On the last call, when no more data remains, simply set bytesRead to zero and return false. Because you may be called upon multiple times for a given file, be sure to track your current read location so you know where you are and how much data has been sent.

For those unfamiliar with the matter, you can store data files directly compiled into your EXE by adding them to your project and setting their "Build Action" to "Embedded Resource", followed by loading their data programmatically using System.Reflection.Assembly.GetManifestResourceStream(). Using the above methods, there is no need to create or read any files from disk.

相关问答

简单的方法(一个“文件”,一页) LoadString()可用于直接从字符串中加载: ChromiumWebBrowser.LoadString(string html, string url);

或者, LoadHtml()可以从给定编码中的字符串中加载: ChromiumWebBrowser.LoadHtml(string html, string url, Encoding encoding);

我尝试了两种方法,他们似乎都能工作,至少在CefSharp.Wpf v51.0.0中 。 根

...

我找到了解决方案,在这里如果有人需要帮助: BrowserSettings browserSettings = new BrowserSettings();

browserSettings.FileAccessFromFileUrlsAllowed = true;

browserSettings.UniversalAccessFromFileUrlsAllowed = true;

browserSettings.TextAreaResizeDisabled = true;

st

...

我终于找到了问题。 由于在单独的进程上运行浏览器以及CefSharp.BrowserSubprocess.exe对CefSharp.dll和CefSharp.Core.dll的依赖性,必须为子进程创建相同的配置文件。 我必须做的唯一更改是创建文件CefSharp.BrowserSubprocess.exe.config并复制与MyApp.exe.config中相同的配置 I finally found the problem. due to running the browser on separ

...

WhatsApp Web检查浏览器功能。 检查navigator.getUserMedia时失败。 要启用WebRTC音频/视频,您需要设置--enable-media-stream标志。 通过CefSettings.CefCommandLineArgs选项以编程方式设置它。 有关示例,请参阅CefSharp.Example / CefExample.cs 。 WhatsApp Web checks for browser capabilities. It fails when checking

...

嗯,我意识到这是几个月前, 看起来你应用的指南和代码是CefSharp1代码分支(该版本确实AFAIK只支持x86)。 注意 CefSharp1和当前master的WPF控制是完全不同的。 刚刚发布的CefSharp 33.0.0我建议您尝试使用该版本的NuGet, 并首先使用CefSharp.MinimalExample的WPF示例运行所有内容。 我认为你使用的指南从那时起已经有所改变了。 虽然不确定它是否已准备好迎接黄金时段。 最后,CefSharp谷歌集团最近发表了一篇关于“DIY版Mini

...

正如马维德女士在评论中指出的那样,对这个确切而明确的问题的正确解决方案是: 在WPF版本中有一个ChromiumWebBrowser.Load(字符串url)。 我认为它也可能在winform版本中。 - Majed DH 5月24日10:29 更具体地说,一个关于如何完成的代码示例如下: public CefSharp.WinForms.ChromiumWebBrowser browser;

public Form1() {

InitializeComponent();

br

...

在CEF中大多数时候用文档操作更容易使用JS。 考虑使用CEF wiki(以及CefSharp示例)来学习如何执行JS并获取结果。 在那之后它将是微不足道的。 Most of times in CEF to manipulate with document is easier to use JS. Consider CEF wiki (and CefSharp samples) to learn how to execute JS and get result back. After that i

...

CefSharp.WinForms.Example具有Find的基本工作实现。 https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.WinForms.Example/BrowserTabUserControl.cs#L253 CEF API文档是http://magpcss.org/ceforum/apidocs3/projects/%28default%29/CefBrowserHost.html#Find%28int,c

...

您可以拦截请求并自行完成。 如果你实现IResourceHandlerFactory然后IResourceHandler (只需在IResourceHandlerFactory.GetResourceHandler返回自定义IResourceHandler的新实例) https://github.com/cefsharp/CefSharp/blob/cefsharp/45/CefSharp/IResourceHandlerFactory.cs#L22 将为每个请求实例化一个新的自定义Resourc

...

来自@amaitland的评论得到了推动。 这是我的实施。 我希望这有助于其他人。 初始化WinForms.ChromiumWebBrowser的实例时,将其属性MenuHandler设置为IContextMenuHandler的实例。 chromeBrowser = New WinForms.ChromiumWebBrowser(uri)

chromeBrowser.MenuHandler = New Classes.CefBasicMenuHandler()

Contr

...

cef调用本地html,在CefSharp中使用本地构建的网页(Working with locally built web page in CefSharp)...相关推荐

  1. java实现本地图片转urljava中映射本地图片地址为url访问

    1.编写一个工具类PhotoUtils实现WebMvcConfigurer,然后重写addResourceHandlers方法即可 @Component public class PhotoUtils ...

  2. android 本地提醒功能,android中的本地定时推送到通知栏

    一.使用系统定义的Notification 以下是使用示例代码: import android.app.Notification; import android.app.NotificationMan ...

  3. linux 虚拟机挂载本地,CentOS 在VMWare中挂载本地yum源

    准备工作 虚拟机 VMWare 系统 CentOS 7 镜像 一.挂载镜像 操作之前点击 虚拟机设置 CD/DVD处勾选 设备状态 [x] 已连接 [x] 启动时连接 挂载镜像 mkdir /mnt/ ...

  4. cefsharp读取exe html,wpf中使用cefsharp加载本地html网页并实现cs和js的交互并且cefsh...

    wpf中使用cefsharp加载本地html网页并实现cs和js的交互并且cefsh wpf中使用cefsharp加载本地html网页并实现cs和js的交互,并且cefsharp支持any cpu 第 ...

  5. java中抓拍图像_JavaCV调用摄像头并抓拍图片保存到本地

    添加依赖 org.bytedeco javacv-platform 1.4.1 org.bytedeco.javacpp-presets opencv-platform 3.4.1-1.4.1 jun ...

  6. 追踪JVM中的本地内存

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 转载自公众号:锅外的大佬 1.概述 有没有想过为什么Java应用程序通过众所周知的-Xms和 ...

  7. SAP UI5 应用开发教程之五十四 - 如何将本地 SAP UI5 应用配置到本地 Fiori Launchpad 中的试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  8. 在InternetExplorer.Application中显示本地图片

    忘记了,喜欢一个人的感觉 Demon's Blog  »  程序设计  »  在InternetExplorer.Application中显示本地图片 « 对VBS效率的再思考--处理二进制数据 Wo ...

  9. this调用语句必须是构造函数中的第一个可执行语句_详解-JavaScript 的 this 指向和绑定...

    JavaScript 中的 new.bind.call.apply 实际这些都离不开 this,因此本文将着重讨论 this,在此过程中分别讲解其他相关知识点. 注意: 本文属于基础篇,请大神绕路.如 ...

  10. java如何调用linux命令_java程序中如何调用linux命令

    java程序中如何调用linux命令 作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服 ...

最新文章

  1. crash工具分析linux内核,如何使用crash工具分析Linux内核崩溃转储文件
  2. 火币网行情获取的websocket客户端
  3. (EM算法)The EM Algorithm
  4. VTK:网格之PointInterpolator
  5. c++ template笔记(1)模板函数
  6. 从FLC中学习的设计模式系列-创建型模式(3)-工厂方法
  7. 图书馆管理系统项目思路
  8. Err:error occurred at recursive SQL level 1
  9. 我最讨厌哪种数据分析师?这四点全中就可以辞职走人了
  10. java 调用三角函数_Java中的三角函数方法
  11. 记录java使用EasyExcel进行单元格内换行操作
  12. 【数据仓库】6.数据质量监控
  13. 不怕新歌有多嗨,就怕老歌带DJ,Python批量对DJ歌曲进行下载
  14. 星际战甲服务器维护时间,星际战甲 官网:2月4日服务器维护结束公告
  15. linux 命令大全_11个炫酷的Linux终端命令大全
  16. 传说之下怎么设置按键_《传说之下手机版》按键设置教程
  17. 什么?这个岗位薪资秒杀一众程序员?
  18. 使用NanoHTTPD在Android上建立本地服务器
  19. 鸿蒙开发中vp和fp是啥?
  20. java条码识别技术_Java 生成、识别条形码

热门文章

  1. lena图像,直方图均衡
  2. IE8中解决Cell华表插件不显示方法!
  3. 国家统计局:政府统计应用大数据的主要障碍
  4. 使用SVD奇异值分解求解PCA+Python实现
  5. matlab南方平差易,测量平差实习心得多篇
  6. 这家自动驾驶公司,或将引领半封闭物流搬运领域的变革
  7. 无线通信设备安装工程概预算编制_电气设备安装工程计价与应用
  8. 【王佩丰】PowerPoint2010视频教程 1
  9. ppt入门到精通全套视频教程,Word+Excel+PPT三合一教程(15G)
  10. 阿里云服务器Discuz论坛程序安装