Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm able to execute a method in my VB.NET program from within the ChromiumWebBrowser with the following code;

Public Class Main

Private WithEvents browser As New ChromiumWebBrowser(IO.Path.GetFullPath("Webpage/index.html")) With {.Dock = DockStyle.Fill}
    Private messageReceiver As New MessageReceiver(browser)

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Controls.Add(browser)
        CefSharpSettings.LegacyJavascriptBindingEnabled = True
        browser.RegisterJsObject("messageReceiver", messageReceiver)
    End Sub
End Class

Public Class MessageReceiver

    Private browser As ChromiumWebBrowser

    Public Sub New(ByRef browser As ChromiumWebBrowser)
        Me.browser = browser
    End Sub

    Public Sub browserToProgram(msg As String)
        ' MsgBox(msg,, "browserToProgram")
    End Sub

    Public Sub send(msg As String)
        browser.ExecuteScriptAsync("alert(" & msg & ")") ' would normally be programToBrowser(msg)
    End Sub

End Class
function browserToProgram(msg)
{
	(async function()
	{
		await CefSharp.BindObjectAsync('messageReceiver', 'bound');
		messageReceiver.browserToProgram(msg);
	})();
}

However, I can't get it working the other way;
Private Sub browser_IsBrowserInitializedChanged(sender As Object, e As IsBrowserInitializedChangedEventArgs) Handles browser.IsBrowserInitializedChanged
        If e.IsBrowserInitialized Then
            Timer1.Start()
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        messageReceiver.send("Test from VB.NET")
    End Sub
function programToBrowser(msg)
{
	alert(msg);
}

So the VB.NET program displays a messagebox (if I uncomment that line) when the JS calls browserToProgram(x), but nothing displays on the webpage when VB.NET calls programToBrowser(x).

What I have tried:

Commenting out .NET-side messageboxes to try to prevent timing issues with threadblocking

Instead of calling the JS method, simply calling 'alert(x)' to eliminate potential issues with user-defined methods
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900