问题
I am trying to load local html/css/js files using CefSharp v65 in WinForms.
I found other stack overflow posts regarding this but none of them used the new built-in version of FolderSchemeHandlerFactory
, instead implementing their own version. Here is the documentation I read on the Scheme Handler: https://github.com/cefsharp/CefSharp/wiki/General-Usage under the "Scheme Handler" header.
Sources: Working with locally built web page in CefSharp
I tried using the new feature like so:
public ChromiumWebBrowser browser;
public void InitBrowser()
{
var settings = new CefSettings();
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = "localfolder",
SchemeHandlerFactory = new FolderSchemeHandlerFactory(
rootFolder: @"..\..\..\..\CEFSharpExample\webpage",
defaultPage: "index.html" // default
)
});
Cef.Initialize(settings);
string html = File.ReadAllText(@"..\..\..\webpage\index.html");
browser = new ChromiumWebBrowser();
browser.LoadHtml(html);
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
However, I am simply getting the html with no css, with no exceptions in the debugger. Does anyone understand how to leverage the new built-in functionality?
回答1:
As pointed out by amaitland in the comments, my "requests weren't being made to the scheme handler as [I was] loading a data URI".
My updated, working code is as follows (a little more fleshed out in case you were wondering where everything was taking place):
public partial class Form1 : Form
{
InitializeComponent();
InitBrowser();
}
public ChromiumWebBrowser browser;
public void InitBrowser()
{
var settings = new CefSettings();
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = "localfolder",
DomainName = "cefsharp",
SchemeHandlerFactory = new FolderSchemeHandlerFactory(
rootFolder: @"C:\CEFSharpExample\webpage",
hostName: "cefsharp",
defaultPage: "index.html" // will default to index.html
)
});
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("localfolder://cefsharp/");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
来源:https://stackoverflow.com/questions/52338368/loading-local-html-css-js-files-with-cefsharp-v65