CEFSharp Winforms 在网站加载时显示加载信息?

CEFSharp Winforms display a loadin message while the website loads?

我正在使用 Cefsharp 39.0.0 在 winform 中加载网页。现在,当页面加载时,表单是空白的。如何显示正在加载的 gif?

public Browser() {
    InitializeComponent();
    string clientURL = "www.imdb.com";

    ChromiumWebBrowser browser = new ChromiumWebBrowser(clientURL);

    browser.Dock = DockStyle.Fill;

    toolStripContainer.ContentPanel.Controls.Add(browser);


    browser.RegisterJsObject("camera", new Camera());

}

我查看了 NavStateChanged 事件。但我不确定如何使用它在表单中显示来自本地资源的加载图像,直到网页完全加载。

EventHandler < NavStateChangedEventArgs > handler = null;
handler = (sender, args) = > {

    //TODO : show a loading gif until the page load completes

    //Wait for while page to finish loading not just the first frame
    if (!args.IsLoading) {
        browser.NavStateChanged -= handler;

        MessageBox.Show("The page has completed loading", "Load completed", MessageBoxButtons.OK);
       //TODO : once load complete show the actual page
    }
};

我们的应用程序是 C++,但其中一些可能适用。我们有一个 html 文件作为资源编译到我们的应用程序中,我们将那个 URL 传递给 CefBrowserHost::CreateBrowserSync(),在你的情况下它看起来像 "new ChromiumWebBrowser()"。我们让启动画面加载我们想要的真实 URL,同时它显示动画 GIF。所以调用可能看起来像

new ChromiumWebBrowser("http://embeddedsplashscreen.html");

我们embeddedsplashscreen.html的body有

<body class="splash" onload=" pageLoad() ">

并且 pageLoad() 看起来像

    function pageLoad() {
        window.location = "www.urlyoureallywant.com";
        }          
    }

我们的启动画面有一个按样式 sheet 设置的背景和一个动画 gif,一旦请求的 url 出现,它就会消失。

对于 CefSharp,我不知道如何指定本地资源。在 C++ 中,我们使用了 cefclient 示例中 resource_util_win.cpp 中的技术,特别是 GetResourceId() 将 URL 与编译后的资源 ID 相关联,它通过匹配 URL 中的文本来实现一个 hard-coded table.

对于 CefSharp,我发现这些链接可能会有帮助 - http://thechriskent.com/2014/05/12/use-embedded-resources-in-cefsharp/CefSharp LoadHtml

Alex Maitland 在 Cefsharp google groups

上的回答

您可以首先从本地磁盘加载您自己的 'Loading Page',这非常快。

第一种方法是使用 ResourceHandler(您可以从磁盘加载流)。 https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/CefExample.cs#L98 (您还应该使用 SchemeHandler,这提供了更多的灵活性,请参阅 https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/CefSharpSchemeHandler.cs#L46

CefSharp 完全无关的选项是在控件上覆盖图像并在完成后将其删除。这可能会提供最佳的用户体验。我敢肯定像 Whosebug 这样的地方在一般意义上有这方面的信息。 请记住,如果您隐藏浏览器实例,它将停止呈现(性能原因),它将继续加载。

如果您不需要代理支持,那么禁用它应该会略微加快初始加载速度(相对而言,代理解析需要一段时间) https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/CefExample.cs#L39