CefSharp : Html from local resource loading, JS,Im

2019-08-28 08:33发布

问题:

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.