Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

430
Vistas
Why won't CefSharp.PostMessage work in CefSharp browser?

I tried using JavascriptMessageReceived event in c# to handle CefSharp.PostMessage called from JS within the browser. I even downloaded an example of this working from link. Ran example which was supposed to do exactly what I needed, but it didn't work, nor does my code (below). Did CefSharp remove this functionality or change it? Do I need to enable this feature within the CefSettings or ChromiumWebBrowser?

using CefSharp;
using CefSharp.WinForms;

public ChromiumWebBrowser chromeBrowser;

public Form1()
    {
        InitializeComponent();

        string curDir = Directory.GetCurrentDirectory();
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);
        chromeBrowser = new ChromiumWebBrowser(String.Format("file:///{0}/HelloWorld.html", curDir));
        chromeBrowser.JavascriptMessageReceived += ChromeBrowser_JavascriptMessageReceived;
    }

private void ChromeBrowser_JavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
    {
        MessageBox.Show((string)e.Message);
    }

HelloWorld.html here

<!DOCTYPE html>
<html>
<body>

<script>
CefSharp.PostMessage("Hello world!");
</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  1. CefSharp.PostMessage("Hello world!"); is a quite new JS extension of 2020. Make sure your are using the actual CefSharp.
  2. chromeBrowser = new ChromiumWebBrowser(String.Format("file:///{0}/HelloWorld.html", curDir));
    chromeBrowser.JavascriptMessageReceived += ChromeBrowser_JavascriptMessageReceived;
    
    The subscription on JavascriptMessageReceived events is too late, the page might be downloaded and JS might be executed to that moment. You should subscribe on events before page load.
    chromeBrowser = new ChromiumWebBrowser("about:blank");
    chromeBrowser.JavascriptMessageReceived += ChromeBrowser_JavascriptMessageReceived;
    chromeBrowser.Load(String.Format("file:///{0}/HelloWorld.html", curDir));
    
  3. Make sure HelloWorld.html is existing in the application current working directory.
about 4 years ago · Juan Pablo Isaza Denunciar

0

Where are you adding the ChromiumWebBrowser to the form? The WinForms ChromiumWebBrowser control won't load until it's handle is created which by default happens when it's added to a parent.

chromeBrowser = new ChromiumWebBrowser(String.Format("file:///{0}/HelloWorld.html", curDir));
chromeBrowser.JavascriptMessageReceived += ChromeBrowser_JavascriptMessageReceived;
Controls.Add(chromeBrowser);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda