" Cef只能初始化一次"当注册js对象时

时间:2016-10-11 07:35:46

标签: chromium-embedded cefsharp

我使用的是CefSharp.WinForm 51.0.0。

当我在Cef.Initialize(settings)之后添加代码registring js对象时,

在运行时Cef.Initialize(settings)发生异常。

  

System.Exception未处理
  的HResult = -2146233088
  Message = Cef只能初始化一次。使用Cef.IsInitialized来防止此异常   来源= CefSharp.Core
  堆栈跟踪:
          위치:CefSharp.Cef.Initialize(CefSettings cefSettings,Boolean shutdownOnProcessExit,Boolean performDependencyCheck)          위치:CefSharp.Cef.Initialize(CefSettings cefSettings)          위치:PcAppCef_WinForm.AppForm.InitializeChromium()파일d:\ VisualStuidoRepo \ StocktalkPC \ PcAppCef_WinForm \ AppForm.cs:줄82          위치:PcAppCef_WinForm.AppForm..ctor()파일d:\ VisualStuidoRepo \ StocktalkPC \ PcAppCef_WinForm \ AppForm.cs:줄53          위치:PcAppCef_WinForm.AppViewport..ctor()파일d:\ VisualStuidoRepo \ StocktalkPC \ PcAppCef_WinForm \ AppViewport.cs:줄15          위치:PcAppCef_WinForm.AppForm.InitializeChromium()파일d:\ VisualStuidoRepo \ StocktalkPC \ PcAppCef_WinForm \ AppForm.cs:줄90          위치:PcAppCef_WinForm.AppForm..ctor()파일d:\ VisualStuidoRepo \ StocktalkPC \ PcAppCef_WinForm \ AppForm.cs:줄53          위치:PcAppCef_WinForm.Program.Main()파일d:\ VisualStuidoRepo \ StocktalkPC \ PcAppCef_WinForm \ Program.cs:줄19

但是,我在.RegisterJsObject()之后调用了Cef.Initialize方法 当我删除(或评论)我的代码chromeBrowser.RegisterJsObject时,效果非常好。

我不明白为什么错误发生在Initialize方法而不是RegisterJsObject

这是我的代码。

 public void InitializeChromium()
        {
            System.Console.WriteLine("*** Initializing Chromium");

            CefSettings settings = new CefSettings();


            Cef.Initialize(settings);

            chromeBrowser = new ChromiumWebBrowser(WebAppUrl);

            chromeBrowser.DownloadHandler = new FileDownloadHandler();
            chromeBrowser.MenuHandler = new ContextMenuHandler();
            //chromeBrowser.RequestHandler = new AppRequestHandler();

            // I want to a add js object but when I add this code, an error occurs on Cef.Initialize() as the blackquote above.
            chromeBrowser.RegisterJsObject("AppViewport", new AppViewport()); 


            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;

        }

1 个答案:

答案 0 :(得分:0)

请您先检查一下姓名的缩写。我认为它可以帮助您:

public void InitializeChromium()
{
    System.Console.WriteLine("*** Initializing Chromium");

    if (!Cef.IsInitialized) // Check before init
    {
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);
    }

    chromeBrowser = new ChromiumWebBrowser(WebAppUrl);

    chromeBrowser.DownloadHandler = new FileDownloadHandler();
    chromeBrowser.MenuHandler = new ContextMenuHandler();
    //chromeBrowser.RequestHandler = new AppRequestHandler();

    // I want to a add js object but when I add this code, an error occurs on Cef.Initialize() as the blackquote above.
    chromeBrowser.RegisterJsObject("AppViewport", new AppViewport());


    this.Controls.Add(chromeBrowser);
    chromeBrowser.Dock = DockStyle.Fill;

}