Developing Desktop Applications using HTML and Javascript...!!!

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 class that implements the BrowserFunction interface:

public class MyFunctionCallback : BrowserFunction
{
public JSValue invoke(params JSValue[] args)
{
return JSValue.Create("Reply from DotNetBrowser");
}
}
All the parameters and return value are instances of JSValue  class, so they can represent any JavaScript type.
The instance of the callback class must be registered inside the Browser instance. That can be done by calling theBrowser.RegisterFunction() method as shown below:
browser.RegisterFunction("MyFunction", new MyFunctionCallback());
  After this call, the callback instance will be mapped with corresponding JavaScript function “MyFunction”
Note: The registered callback becomes accessible from the loaded web page if and only if it was registered before the web page was loaded. The list of registered functions is checked every time when browser loads a web page or frame. Therefore, if the web page was loaded before registering JavaScript function, it will be necessary to reload the page before trying to access the callback.
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.
JSValue returnValue = browser.ExecuteJavaScriptAndReturnValue("MyFunction();");
if (returnValue.IsString())
{
string replyFromDotNetBrowser = returnValue.GetString();
// replyFromDotNetBrowser = "Reply from DotNetBrowser"
}
From JavaScript on the web page you can call this callback function using the following code:
<script>
var result = MyFunction();
</script>
To unregister callback function call the 
Browser.UnregisterFunction() method:


When the callback is unregistered, JavaScript cannot invoke it anymore and any attempt to access it will cause JavaScript error.
The demonstrated approach allows you to implement rather complex and flexible interactions between JavaScript and C#.

NilDev

Hello Ninjas, I am Nildev Vaghela ( professional blogger ) studying in B.E Mechanical. I like to descover more easy and fantastic blogger stunts for better blogging and designing. Continue your Fight here with NinjasTech to Be Pro-Blogger from Pre-Blogger..!!

Post a Comment

Previous Post Next Post