首页 > 解决方案 > CefSharp 不会加载 HTML

问题描述

我知道这已经被问过很多次了,但是那里建议的解决方案都没有奏效。我正在尝试通过 nuget 在最新版本的 cefsharp/offscreen 中提供 cefsharp 自定义 html。这是代码示例:

        private void Start(string cachePath)//unsafe tp call twice currently
    {
        var settings = new CefSettings()
        {
            //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
            CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
        };
        //Now for the internals

        Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
        Thread.Sleep(500);
        browser = new ChromiumWebBrowser("dummy:");
        Thread.Sleep(500);
    }


    private int CleanMemory()
    {
        Cef.Shutdown();
        return 1;
    }

    private async void TakePictures()
    {
        //Reduce rendering speed to one frame per second so it's easier to take screen shots
        //browserSettings.WindowlessFrameRate = 1;

        while (m_ReceivedObjects.Any() && currentImageToBeProcessed.IsRendered == false)
        {
            currentImageToBeProcessed = m_ReceivedObjects.Dequeue();

            //await LoadPageAsync(browser, currentImageToBeProcessed.filePath, currentImageToBeProcessed.rawHtml);
            currentImageToBeProcessed.IsRendered = true;
            string h = "< html >< head >< style > body { background - color: #93B874; } h1 { background - color: #00b33c; } p { background - color: #FFFFFF); } </ style >"
                + "</ head > < body >< h1 > Header with Green Background </ h1 >< p > Paragraph with white background </ p ></ body ></ html > ";

            //CefSharp.WebBrowserExtensions.LoadHtml(browser, h, "http://www.example.com/");
            //browser.LoadHtml("<html><body><h1>Hello world!</h1></body></html>", "http://example.com/");
            //browser.Load("https://www.google.com/");
            browser.Load("data:,Hello%2C%20World!");// LoadHtml("<html><body><h1>Hello world!</h1></body></html>", "http://example.com");
            //browser.Load("data:text/html," + System.Web.HttpUtility.UrlPathEncode(currentImageToBeProcessed.rawHtml));

            Thread.Sleep(2000);
            await browser.ScreenshotAsync().ContinueWith(processHTML);
        }
    }

在底部附近,带有注释的行是问题所在。

我已经尝试了 loadHtml、loadString 和加载各种不同的 url 和数据 uri 类型。没有一个工作。

谷歌页面和所有“实际”网址都可以工作。(在我有更多时间之前,2 秒睡眠是一个占位符。)

标签: c#browsercefsharpcefsharp.offscreen

解决方案


推荐阅读