Over a million developers have joined DZone.

JavaScript in .NET Desktop Applications

DZone's Guide to

JavaScript in .NET Desktop Applications

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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:

window.Account.Save("FirstName", "LastName");

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

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.

html5 ,.net ,javascript

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}