CefSharp : Html from local resource loading, JS,Images not loading

自古美人都是妖i 提交于 2020-01-06 17:58:39

问题


I am working on a CefSharp application in which we are trying to load content locally. It's partially working for me, as it's only loading HTML. Any type of CSS, JS, images are not loaded. I have tried adding multiple paths, even tried directly adding it in the same resources folder, but it doesn't work. What am I doing wrong?

Code :

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

Thank you.

来源:https://stackoverflow.com/questions/52055031/cefsharp-html-from-local-resource-loading-js-images-not-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!