我正在开发一个CefSharp应用程序,我们正在其中尝试在本地加载内容。它对我来说部分起作用,因为它仅加载HTML。不会加载任何类型的CSS,JS,图像。我尝试添加多个路径,甚至尝试将其直接添加到同一资源文件夹中,但是它不起作用。我在做什么错了?
代码:
namespace OurApp
{
public partial class OurApp : Form
{
public ChromiumWebBrowser chromiumBrowser;
FileResourceHandlerFactory fileResourceHandlerFactory = new FileResourceHandlerFactory("app","local","resources","test.html");
public OurApp()
{
InitializeComponent();
CefSettings settings = new CefSettings();
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = "app",
SchemeHandlerFactory = fileResourceHandlerFactory,
IsSecure = false
});
settings.UserAgent = "app,win";
settings.PersistSessionCookies = true;
Cef.Initialize(settings);
InitializeChromium();
chromiumBrowser.TitleChanged += OnBrowserTitleChanged;
}
private void InitializeChromium()
{
chromiumBrowser = new ChromiumWebBrowser(string.Empty)
{
Dock = DockStyle.Fill,
};
chromiumBrowser.Size = new Size(1080, 1920);
string curDir = Directory.GetCurrentDirectory();
chromiumBrowser.Load("app://local");
}
//Resource handler :
public class FileResourceHandlerFactory : ISchemeHandlerFactory
{
private string scheme, host, folder, default_filename;
public string Scheme => scheme;
public FileResourceHandlerFactory(string scheme, string host, string folder, string default_filename = "test.html")
{
this.scheme = scheme;
this.host = host;
this.folder = folder;
this.default_filename = default_filename;
}
private string get_content(Uri uri, out string extension)
{
var path = uri.LocalPath.Substring(1);
path = string.IsNullOrWhiteSpace(path) ? this.default_filename : path;
extension = Path.GetExtension(path);
return File.ReadAllText(Path.Combine(this.folder, path));
}
IResourceHandler ISchemeHandlerFactory.Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
var uri = new Uri(request.Url);
return ResourceHandler.FromString(get_content(uri, out var extension), extension);
}
}
}
谢谢。