CefWebBrowser不显示(CefWebBrowser doesn't show up

2019-08-07 06:37发布

解:

此问题是由不加载在Program.cs中CefRuntime引起的。 简单地复制示例文件到您的Program.cs的所有代码。

然后如果你CefWebBrowser被卡住,并具有“自旋等待鼠标光标”,然后将SingleProcess =真。

原因是,如果浏览器从一个公认的二次加工叫,整个过程会被拦截,直到过程结束。


题:

任何人有在winform使用CEF(铬嵌入式框架)的经验?

此组件已经折磨了我整整一天。 我不能使它发挥作用。 它只是简单地不露面。

所以我的问题是:如何在WinForm应用程序使用CEF?

我做了检查一下论坛,但令人惊讶的没有人问过这个问题。 好像只有我一个哑巴,谁不知道如何使用它。

public partial class Form1 : Form
{
    private CefWebBrowser cefwbShell = null;
    //private readonly SynchronizationContext _pUIThread;

    private void Form1_Load(object sender, EventArgs e)
    {
        cefwbShell.Visible = true;
        cefwbShell = new CefWebBrowser { StartUrl = "http://example.com" };
        cefwbShell.Parent = this;
        cefwbShell.Dock = DockStyle.Fill;
        cefwbShell.BringToFront();
        cefwbShell.Show();
    }
}

更新:

我试图用Xilium.CefGlue 。 我复制释放的dll的正确版本后(991)我得到一个异常:

InvalidOperationException was unhandlled by user code 
   Failed to create browser.

来源: Xilium.CefGlue

堆栈跟踪:

at Xilium.CefGlue.CefBrowserHost.CreateBrowser(CefWindowInfo windowInfo, CefClient client, CefBrowserSettings settings, String url) in C:\Winston\Knowledge\Projects\xilium-xilium.cefglue-61551ec98ad8\xilium-xilium.cefglue-61551ec98ad8\CefGlue\Classes.Proxies\CefBrowserHost.cs:line 37
   at Xilium.CefGlue.WindowsForms.CefWebBrowser.OnHandleCreated(EventArgs e) in C:\Winston\Knowledge\Projects\xilium-xilium.cefglue-61551ec98ad8\xilium-xilium.cefglue-61551ec98ad8\CefGlue.WindowsForms\CefWebBrowser.cs:line 71
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
   at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)

任何人都可以帮助我吗?

更新2:

private void Create()
        {
            var page = new TabPage("New Tab");
            page.Padding = new Padding(0, 0, 0, 0);

            var browser = new CefWebBrowser();
            browser.StartUrl = startUrl;
            browser.Dock = DockStyle.Fill;
            browser.TitleChanged += (s, e) =>
            {
                BeginInvoke(new Action(() =>
                {
                    var title = browser.Title;
                    if (tabControl.SelectedTab == page)
                    {
                        Text = browser.Title + " - " + _mainTitle;
                    }
                    page.ToolTipText = title;
                    if (title.Length > 18)
                    {
                        title = title.Substring(0, 18) + "...";
                    }
                    page.Text = title;
                }));
            };
            browser.AddressChanged += (s, e) =>
            {
                BeginInvoke(new Action(() =>
                {
                    addressTextBox.Text = browser.Address;
                }));
            };
            browser.StatusMessage += (s, e) =>
            {
                BeginInvoke(new Action(() =>
                {
                    statusLabel.Text = e.Value;
                }));
            };

            page.Controls.Add(browser);

            tabControl.TabPages.Add(page);

            tabControl.SelectedTab = page;
        }

Answer 1:

我也曾经有过在启动浏览器的问题。 我可以加载所有的DLL CEF的,但是浏览器不会显示出来! 所有我徘徊在控制以上时是旋转等待鼠标光标。

不幸的是我还没有找到问题的根源,但由于样本项目CefGlue.Client工作,我只需将其复制到我的解决方案来代替。

另外,我不知道你是如何初始化CEF运行。 在Program.cs中的CefGlue.Client看看它是如何做的,但它基本上是这样的:

    [STAThread]
    private static int Main(string[] args)
    {
        try
        {
            CefRuntime.Load();
        }
        catch (DllNotFoundException ex)
        {
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 1;
        }
        catch (CefRuntimeException ex)
        {
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 2;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 3;
        }

        var mainArgs = new CefMainArgs(args);
        var app = new DemoApp();

        var exitCode = CefRuntime.ExecuteProcess(mainArgs, app);
        if (exitCode != -1)
            return exitCode;

        var settings = new CefSettings
            {
                // BrowserSubprocessPath = @"D:\fddima\Projects\Xilium\Xilium.CefGlue\CefGlue.Demo\bin\Release\Xilium.CefGlue.Demo.exe",
                SingleProcess = false,
                MultiThreadedMessageLoop = true,
                LogSeverity = CefLogSeverity.Disable,
                LogFile = "CefGlue.log",
            };

        CefRuntime.Initialize(mainArgs, settings, app);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        if (!settings.MultiThreadedMessageLoop)
        {
            Application.Idle += (sender, e) => { CefRuntime.DoMessageLoopWork(); };
        }

        Application.Run(new MainForm());

        CefRuntime.Shutdown();
        return 0;
    }


Answer 2:

你不应该使用SingleProcess进行生产,实际上你可以离开多进程模式( SingleProcess=false ),但需要禁用Visual Studio Hosting Process ,你不会有问题的调试(旋转等待光标)



Answer 3:

你永远不添加控件到窗体的控件集合,至少你表现出的样本英寸



Answer 4:

我也有同样的问题,我解决它通过修改“BrowserSubprocessPath”,该defualt是这样的:

        //var browserProcessPath = CombinePaths(localFolder, "..", "..", "..",
        //    "CefGlue.Demo.WinForms", "bin", "Release", "Xilium.CefGlue.Demo.WinForms.exe");

        var browserProcessPath = CombinePaths(localFolder, "Xilium.CefGlue.Demo.WinForms.exe");

        var settings = new CefSettings
        {
            BrowserSubprocessPath = browserProcessPath,
            SingleProcess = false,
            MultiThreadedMessageLoop = true,
            LogSeverity = CefLogSeverity.Disable,
            LogFile = "CefGlue.log",
        };

因为我改变了应用程序的输出目录,所以“browserProcessPath”是无效的,那么我修改“browserProcessPath”相匹配的输出目录,以便应用程序能找到它。

但是,我仍然无法理解“browserProcessPath”的意义和用法



文章来源: CefWebBrowser doesn't show up