Search code examples
c#javascripthtmlwinformsgeckofx

Calling c# methods from GeckoFX C# controll (winforms)


I want to make an application that uses HTML5 as the UI. The code behind should be written on C#.Net

I have a GeckoFX controll. The thing I want to do is when I click a button/div element in the HTML page, a C# method to be activated.

Here is my idea made-up in code:

HTML

<div id="testBtn" onclick="doSomething()">Click for a C# generated message </div>

C#

void doSomething()
{
//does something in C#
}

Solution

  • Rather than add the event handler using javascript you can add the event handler in C#.

    <div id="testBtn">Click for a C# generated message </div>
    

    C#:

    browser.DomClick += (sender, args) =>
            {
                switch (args.Target.CastToGeckoElement().GetAttribute("id"))
                {
                    case "testBtn":
                        MessageBox.Show("does something in C#");
                        break;
                }
            };
    

    Now it would be nice to able to do something like:

    browser.Document.GetElementById("testBtn").DomClick += (s,a) => doSomething();

    But currently geckofx doesn't have this feature, although it would be possible to add...