CefSharp无法加载HTML

时间:2018-09-19 19:51:15

标签: c# browser cefsharp cefsharp.offscreen

我知道之前曾多次问过这个问题,但是那里提出的解决方案都没有奏效。我正在尝试通过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类型进行加载。没有一个起作用。

虽然google页面和所有“实际”网址都可以使用。 (2秒钟的睡眠是占位符,直到我有更多时间。)

0 个答案:

没有答案