使用CefSharp v65加载本地html / css / js文件 [英] Loading local html/css/js files with CefSharp v65

查看:1608
本文介绍了使用CefSharp v65加载本地html / css / js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WinForms中使用CefSharp v65加载本地html / css / js文件。

I am trying to load local html/css/js files using CefSharp v65 in WinForms.

我发现了与此相关的其他堆栈溢出帖子,但没有一个使用 FolderSchemeHandlerFactory 的新内置版本,而不是实现自己的版本。这是我在方案处理程序中阅读的文档: https://github.com/cefsharp / CefSharp / wiki / General-Usage 在方案处理程序标题下。

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.

来源:在CefSharp中使用本地构建的网页

像这样使用新功能:

    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;

    }

但是,我只是得到没有CSS的html,调试器中没有例外。有人知道如何利用新的内置功能吗?

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?

推荐答案

正如amaitland在评论中指出的那样,我的请求

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;   
}

这篇关于使用CefSharp v65加载本地html / css / js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆