c# - CefSharp 不会加载 HTML
问题描述
我知道这已经被问过很多次了,但是那里建议的解决方案都没有奏效。我正在尝试通过 nuget 在最新版本的 cefsharp/offscreen 中提供 cefsharp 自定义 html。这是代码示例:
private void Start(string cachePath)//unsafe tp call twice currently
{
var settings = new CefSettings()
{
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
};
//Now for the internals
Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
Thread.Sleep(500);
browser = new ChromiumWebBrowser("dummy:");
Thread.Sleep(500);
}
private int CleanMemory()
{
Cef.Shutdown();
return 1;
}
private async void TakePictures()
{
//Reduce rendering speed to one frame per second so it's easier to take screen shots
//browserSettings.WindowlessFrameRate = 1;
while (m_ReceivedObjects.Any() && currentImageToBeProcessed.IsRendered == false)
{
currentImageToBeProcessed = m_ReceivedObjects.Dequeue();
//await LoadPageAsync(browser, currentImageToBeProcessed.filePath, currentImageToBeProcessed.rawHtml);
currentImageToBeProcessed.IsRendered = true;
string h = "< html >< head >< style > body { background - color: #93B874; } h1 { background - color: #00b33c; } p { background - color: #FFFFFF); } </ style >"
+ "</ head > < body >< h1 > Header with Green Background </ h1 >< p > Paragraph with white background </ p ></ body ></ html > ";
//CefSharp.WebBrowserExtensions.LoadHtml(browser, h, "http://www.example.com/");
//browser.LoadHtml("<html><body><h1>Hello world!</h1></body></html>", "http://example.com/");
//browser.Load("https://www.google.com/");
browser.Load("data:,Hello%2C%20World!");// LoadHtml("<html><body><h1>Hello world!</h1></body></html>", "http://example.com");
//browser.Load("data:text/html," + System.Web.HttpUtility.UrlPathEncode(currentImageToBeProcessed.rawHtml));
Thread.Sleep(2000);
await browser.ScreenshotAsync().ContinueWith(processHTML);
}
}
在底部附近,带有注释的行是问题所在。
我已经尝试了 loadHtml、loadString 和加载各种不同的 url 和数据 uri 类型。没有一个工作。
谷歌页面和所有“实际”网址都可以工作。(在我有更多时间之前,2 秒睡眠是一个占位符。)
解决方案
推荐阅读
- javascript - 如何使用 Node.js Cassandra 驱动程序运行连续查询?
- google-cloud-platform - GCP:更改谷歌云平台SQL实例的时区
- ruby-on-rails - 面临 NoMethodError 且无法查明源头
- javascript - 设置默认值以响应 boostrap Form.Check
- tensorflow - 从 TF1 移动到 TF2 时具有多个输入的功能模型中的 AssertionError
- spring - 使用 Spring Boot Java 端口 8080 上的 Tomcat 失败
- php - 如何解决:“此路由不支持 GET 方法。支持的方法:POST。”
- postgresql - 使用部分索引更新和插入性能
- image-charts - Image-Charts 雷达图的微调
- algorithm - 排序后的数组,如果元素在中有多少比较