简体   繁体   English

Interop用户控件中的CEFSharp(铬嵌入式框架)

[英]CEFSharp (Chromium Embedded Framework) in Interop User Control

I am trying to use the CefSharp CLR bindings for the Chromium Embedded Framework to create an alternative web browser control that we can embed into an application that only supports legacy ActiveX controls (WonderWare InTouch HMI) in an attempt to gain some HTML5 support for some reeaaallly old machines. 我正在尝试对Chromium嵌入式框架使用CefSharp CLR绑定来创建替代的Web浏览器控件,我们可以将其嵌入到仅支持旧版ActiveX控件(WonderWare InTouch HMI)的应用程序中,以期获得对某些内容的HTML5支持。旧机器。

So far, I've created an Interop User Control using the Microsoft InteropForms Toolkit and embedded a CefSharp.WinForms WebView onto the control in a pretty bare-bones manner, ie 到目前为止,我已经使用Microsoft InteropForms工具包创建了一个Interop用户控件,并将CefSharp.WinForms WebView以一种非常准的方式嵌入到该控件中,即

Private Sub WebControl_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim Settings = New CefSharp.Settings
    If (CEF.Initialize(Settings)) Then
        WebView = New WebView(homepage, New BrowserSettings())
        WebView.Dock = DockStyle.Fill
    End If

    Me.Controls.Add(WebView)

End Sub

When I build the DLL, I register it via the command 生成DLL时,我通过以下命令进行注册

regasm /tlb /codebase Cefsharp.WebControl.dll 加气/ tlb /代码库Cefsharp.WebControl.dll

This seems to work for the most part, but I am having some issues that I do not fully understand. 这似乎在大多数情况下都有效,但是我遇到了一些我不完全理解的问题。 I am testing it with the VBA Forms in Excel (purely out of convenience) and when I am using the Form Builder, it actually initializes the control when it is added to the user form and loads the webpage perfectly: 我正在用Excel中的VBA表单进行测试(纯粹出于方便),当我使用表单生成器时,实际上是在将控件添加到用户表单并完美加载网页时初始化了该控件:

IDE范例

However, when I try to show the UseForm it seems to either destroy the handle or the instance of the webview (or both) and I can't figure out how to fix it. 但是,当我尝试显示UseForm时,似乎破坏了句柄或webview的实例(或同时破坏了两者),并且我不知道如何修复它。 Once I try to show the form and try something like 一旦我尝试显示表单并尝试类似

Sub Test()
    WebForm.Show
    WebForm.WebControl.Navigate "www.google.com"
End Sub

I get an error that the object doesn't exist. 我收到该对象不存在的错误。 I've messed around a bit and sometimes I get different errors like the pointer is invalid but so far I haven't figured out how to set it up properly. 我有些混乱,有时会遇到不同的错误,例如指针无效,但到目前为止,我还没有弄清楚如何正确设置它。 I think it is a problem with handling the User Form events, but I am not sure. 我认为处理用户表单事件存在问题,但是我不确定。

If anyone has any insight on this, I'd greatly appreciate it. 如果有人对此有任何见解,我将不胜感激。 Thanks! 谢谢!

Okay, I figured it out! 好吧,我知道了! I think it partly had to do with the handler and partly had to do with how/when I was initializing the webview. 我认为这部分与处理程序有关,部分与我初始化Webview的方式/时间有关。 The solution I went with ended up using the CefSharp.Wpf instead of the Windows Form, but it's mostly the same. 我所使用的解决方案最终使用CefSharp.Wpf而不是Windows Form,但是基本上是相同的。 The main difference is adding the initialization of an ElementHost control on the WebControl Designer and create the new WebView as part of the component initialization: 主要区别是在WebControl设计器上添加ElementHost控件的初始化,并创建新的WebView作为组件初始化的一部分:

'Do not change this subroutine in the code editor. Use the designer.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.WebHost = New System.Windows.Forms.Integration.ElementHost()
    Me.SuspendLayout
    '
    'WebHost
    '
    Me.WebHost.Dock = System.Windows.Forms.DockStyle.Fill
    Me.WebHost.Location = New System.Drawing.Point(0, 0)
    Me.WebHost.Name = "WebHost"
    Me.WebHost.Size = New System.Drawing.Size(404, 244)
    Me.WebHost.TabIndex = 0
    Me.WebHost.Text = "WebHost"
    Me.WebHost.Child = New WebView(homepage, New CefSharp.BrowserSettings)
    '
    'WebControl
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 13!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.Controls.Add(Me.WebHost)
    Me.Name = "WebControl"
    Me.Size = New System.Drawing.Size(404, 244)
    Me.ResumeLayout(false)

End Sub

Friend WithEvents WebHost As System.Windows.Forms.Integration.ElementHost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM