DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. JavaScript in .NET Desktop Applications

JavaScript in .NET Desktop Applications

Anna Dolbina user avatar by
Anna Dolbina
·
Jun. 06, 15 · Interview
Like (1)
Save
Tweet
Share
4.93K Views

Join the DZone community and get the full member experience.

Join For Free

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

JavaScript application Desktop (word processor)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Keep Your Application Secrets Secret
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Simulating and Troubleshooting BLOCKED Threads in Kotlin [Video]
  • How To Build a Spring Boot GraalVM Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: