从浏览器执行方法/功能,反之亦然?

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

我目前正在为 WinForms 使用 CefSharp 111.2.20。我让浏览器加载我的 html/css 页面。我希望能够单击页面上的一个元素并让该元素执行 js 代码以在我的 vb.net 代码中运行一个函数。

一直在网上找,好像要用

browser.JavascriptObjectRepository.Register
.

但是,即使在检查 ObjectRepository 是否与

browser.JavascriptObjectRepository.IsBound("hello")
绑定之后,当我点击我的 bnutton 时也没有任何反应。

Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    CefSharpSettings.WcfEnabled = True
    browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = True
    browser.JavascriptObjectRepository.Register("hello", New DotNetMessage(), False, Nothing)

    Me.Controls.Add(browser)
    browser.Dock = DockStyle.Left
    browser.Width = 230

    AddHandler browser.IsBrowserInitializedChanged, AddressOf IsBrowserInit

End Sub
    Private Sub IsBrowserInit(sender As Object, e As EventArgs)
        browser.Load("C:\Users\chuynh\source\repos\AMPv3\AMPv3\bin\Debug\sidebar\index.html")

        If browser.JavascriptObjectRepository.IsBound("hello") Then
            MessageBox.Show("Bound")
        Else
            MessageBox.Show("Not Bound")
        End If
    End Sub

Public Class DotNetMessage
    Public Sub ShowMsg(Message As String)
        MessageBox.Show(Message)
    End Sub
End Class

这是一个带有按钮的简单 html 页面

<head>
    <script>
function ShowDotNetMessage(){
    hello.show("Hello World");
}
    </script>
</head>
<input type="button" id="myButton" onclick="ShowDotNetMessage();" value="Click Me">

当我点击按钮时没有任何反应。

有人能让这个工作吗?或者反过来呢,我可以从 vb.net 调用 JS 函数?

vb.net cefsharp
© www.soinside.com 2019 - 2024. All rights reserved.