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 error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
html5 ,.net ,javascript

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}