Using JavaScript In Your Windows 10 App

js logo

Happy new year friends!

In our blog post today, I wanted to introduce you to a helpful open source project called NIL.JS that enables Windows 10 developers to embed the JavaScript language into their applications. You can quickly install this library in your Windows 10 app using the NuGet package manager.  So, why would you use this library?

  • Expression evaluation: While working on a small business workflow application, I’ve used a JavaScript library like this to evaluate JavaScript expressions at run time.
  • Defining behavior at run time: Over the Christmas break, I’ve been writing a Blockly tool to empower young makers using Microsoft Hololens.   In my case, I needed a way to run a JavaScript program to control characters in the game world.   The users will define the behavior of the agents at run time.
  • Tweaking code at run time: While I’m a big fan of making applications for Hololens using Unity and VS.NET, I’m not a fan of the two-stage development experience.   In stage one, you need to design your game experiences using Unity 3D.    In the second stage, you build and deploy your work to a visual studio solution.   This stage enables you to use the full power of the Windows 10 environment.   If you need to iterate on small code changes to C# behaviors or code, the “compile, deploy, run” model breaks your momentum.

 

Here’s a few code examples showing the utility of NIL.JS.

Getting Values From JavaScript Environment Value Value

You can find the following code and other examples in the Github account. In this code sample, we create a JavaScript context and assign a variable “x” the value 123. In JavaScript code, the variable ‘result’ is assigned x times two. As a C# developer, you can access the result variable and inspect the type.

 


using System;
using NiL.JS.Core;

namespace Examples.Get_values_from_JavaScript_environment
{
public sealed class Via_Value : ExamplesFramework.Example
{
public override void Run()
{
var context = new Context();

context.DefineVariable("x").Assign(123);
context.Eval("var result = x * 2");

object result = context.GetVariable("result").Value;

Console.WriteLine("result: " + result); // Console: result: 246

Console.WriteLine("Type of result: " + result.GetType()); // Console: Type of result: System.Int32
}
}
}

Pass Delegate into JavaScript Environment

In C#, delegates enable you to define pointers to methods. In the first line, the author defines a delegate to a method that will show a message to the screen. Using the “DefineVariable” method on the context object, you can define a function that will be callable in JavaScript. NIL.JS makes it nice and easy.


using System;
using NiL.JS.Core;

namespace Examples.Methods_and_Events
{
public sealed class Pass_delegate_into_JavaScript_environment : ExamplesFramework.Example
{
public override void Run()
{
var @delegate = new Action(text => System.Windows.Forms.MessageBox.Show(text));
var context = new Context();

context.DefineVariable("alert").Assign(JSValue.Marshal(@delegate));
context.Eval("alert('Hello, World!')"); // Message box: Hello, World!
}
}
}

You can learn more about NIL.JS on GitHub.

We enjoy hearing from our readers!  Let us know how you might use NIL.JS in your application by leaving a comment below.

 

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*