如何从JavaScript调用打开新的winform?

问题描述 投票:0回答:1

我与CEFSHARP有我的主要问题目前是,我无法弄清楚如何使用cefsharp铬浏览器控件中的JavaScript来生成一个新的Windows窗体。

我想一个解决方案在谷歌搜索,但我找不到任何相关的努力在一个页面点击一个链接/按钮时生成一个新的Windows窗体。基本上从JavaScript页面回话给vb.net客户端。

从我读,我相信这件事情有关,其中主要形式的UI主线程不能通讯的JavaScript对象注册一个正确线程问题。

我目前在VB(VS2015)的代码如下:

Imports System.Windows.Forms
Imports System.Security.Permissions
Imports CefSharp.WinForms
Imports CefSharp

Public Class Form1

    Private WithEvents browser As ChromiumWebBrowser
    Friend Shared MyInstance As Form1

    Public Sub New()
        InitializeComponent()

        Dim settings As New CefSettings()
        CefSharp.Cef.Initialize(settings)

        browser = New ChromiumWebBrowser("/test.php") With {
            .Dock = DockStyle.Fill
        }

        webcontrol.Controls.Add(browser)

        browser.JavascriptObjectRepository.Register("CallBrowser", New InteractAPP(), True)

    End Sub

    Public Sub SpawnForm()
        Dim myPop As New Form2
        myPop.Show()


    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        MyInstance = Me

    End Sub
End Class

Public Class InteractAPP
    Public Sub showMessage()

        If (Form1.MyInstance IsNot Nothing) AndAlso Not Form1.MyInstance.IsDisposed Then
            MsgBox("It Works!")
        End If

    End Sub

    Public Sub createWin()
    form1.MyInstance.SpawnForm()

    End Sub

    Public Sub doHide()
        If (Form1.MyInstance IsNot Nothing) AndAlso Not Form1.MyInstance.IsDisposed Then
            Form1.MyInstance.Close()
        End If
    End Sub

End Class

我有我的一般网页上的代码如下:

<html>
<head>
<title></title>
<script>

    // Setup communication to web browser control
    (async function() {
        await CefSharp.BindObjectAsync("CallBrowser", "bound");
    })();
    // calling ends here

    function doClose()
    {
        CallBrowser.doHide();
    }

    function openWindow()
    {

        //ByVal url As String, ByVal title As String, ByVal w As Integer, ByVal h As Integer, ByVal freeze As Integer, ByVal loadMaximize As Integer
        CallBrowser.createWin();
    }

</script>
</head>
<body>
<input type='button' value='Close Win' onClick='doClose();'/>
<input type='button' value='Spawn Popup' onClick='openWindow();'/>
</body>
</html>

当我点击引用的openWindow()的网页菌种弹出按钮,没有任何反应,当调用该函数在VB。

我不能就如何得到这个正常工作在任何地方找到的任何信息。

vb.net winforms cefsharp chromium-embedded
1个回答
2
投票

因为所有通过CallBrowser对象从你的JavaScript执行的动作在一个单独的非UI线程的运行,你看到的行为是正常的。所以,你在哪里合适的时候,你说这是一个线程问题。

要存档,你有你想要的东西,创建从UI线程的新窗口。您可以使用InvokeBeginInvoke方法来拥有你已经(从你的样品Form1的实例)控制的线程上运行的代码。这里是你的SpawnForm子可怎么是这样的:

Delegate Sub InvokeDelegate()

Public Sub SpawnForm()

    Me.BeginInvoke(New InvokeDelegate(
        Sub()
            Dim myPop As New Form2
            myPop.Show()
        End Sub
    ))

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.