Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JavaScript in .NET Desktop Applications

DZone's Guide to

JavaScript in .NET Desktop Applications

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

There are a lot of nice and powerful HTML5 UI toolkits that can be used to build gorgeous applications. Why not use them to build good looking desktop applications? Significant development time was invested in creating such UI toolkits and I believe it’s desirable to reuse them in .NET desktop applications.

DotNetBrowser library is one of the most advanced library on the market that can help you in building .NET desktop applications built with HTML5 UI toolkits and configuring communication between your C# code and JavaScript code on a web page loaded in DotNetBrowser’s WPF/WinForms component.

In this article I will show how to invoke C# code from JavaScript:
First, you need to implement the .NET callback class. Callback class is a regular C# class, but its methods accept only .NET primitives, JSObject, JSArray or JSFunction: 

public class Account
        {
            public void Save(String firstName, String lastName)
            {
                Console.Out.WriteLine("firstName = " + firstName);
                Console.Out.WriteLine("lastName = " + lastName);
            }
        }


JSObject, JSArray or JSFunction are used to represent JavaScript types.

The instance of the callback class must be registered inside the Browser instance. That can be done by setting the object instance as a property of the existing JS object  as shown below:

JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
value.AsObject().SetProperty("Account", new Account());

After this call, the callback instance will be mapped with corresponding JavaScript object “window.Account”

Note: The callback object can be registered if and only if the web page was loaded completely. In addition, JavaScript code can access/invoke only public methods of registered .NET object.

When the web page is loaded and the sample callback is already accessible from it, we can invoke the registered JavaScript function from the .NET side, just to demonstrate the interaction.

browser.ExecuteJavaScript("window.Account.Save('FirstName', 'LastName');");

From JavaScript on the web page you can call this callback function using the following code:

<script>
window.Account.Save("FirstName", "LastName");
</script>

The demonstrated approach allows you to implement rather complex and flexible interactions between JavaScript and C#.

Useful links:

About library: http://www.teamdev.com/dotnetbrowser
Samples: https://sites.google.com/a/teamdev.com/dotnetbrowser-support/samples

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
html5 ,.net ,javascript

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}