By Philippe Leefsma
As I’m planning to get some experience in Windows 8 programming I thought I was going to give it a try last week. One of the new way to develop Windows Store Apps, as Microsoft calls it, is to write your app using JavaScript, HTML and CSS code which opens some quite interesting perspectives comparing to what we are used to in term of desktop or even mobile apps and also may attract a different crowd into programming, mainly thinking here about web developer-like profiles.
After having skimmed the Hello, World! tutorial on JavaScript app from Microsoft, I decided to go ahead and create my first Windows 8 app. I needed an idea and a good candidate was to migrate the 2d HTML5 canvas script I created few weeks ago: Giving a try at HTML5 canvas and Javascript.
I was surprised how straightforward and quick it ended up to be. It basically took me three simple steps to get it running:
#1 - Create a new JavaScript –> Windows Store App project from Visual Studio 2012. You need to have installed the Windows 8 SDK and important consideration, you need to develop on a Windows 8 OS, here are the words from Microsoft: Windows Store app development in Visual Studio is supported only on Windows 8. Windows 7 is not supported. In addition, developer licenses aren't available for Windows Server 2012, so you can't develop Windows Store apps on that operating system.
Several files and directories are created by the default project template. You can see them below:
![Screen shot 2013-01-21 at 11.55.37 AM_thumb[4] Screen shot 2013-01-21 at 11.55.37 AM_thumb[4]](https://adndevblog.typepad.com/.a/6a0167607c2431970b017c36180f55970b-pi)
Like in a .Net project, the References folder contains libraries needed by your app. We can see here the Windows Library for JavaScript 1.0. A brand new library of components provided by Microsoft on top of JavaScript that we will see in a bit more details.
Then two folders that web developers are familiar with: CSS and js that will contain our .css and .js files.
The .html file that will contain images folder for our image resources, and a defaultthe html code being display by default at start up by our app.
The only thing I was doing here is taking the JavaScript code from my previous post and isolate it inside it’s own file: JsCAD.js
-
#2 - Because JavaScript doesn’t provide native support for namespaces and using global variables is always a bad practice, even for a small application, I gathered all the JsCAD methods and variables under a namespace-like structure as follow:
var JsCAD = {};
JsCAD.MemberFunction = function (args) {
//Function body...
JsCAD.MemberVariable = something…
}
I then edited the default.js file that contains the code related to default.html. Here is the content of this file, it provides callbacks invoked automatically by the framework. I simply placed a call in order to initialize my code (highlighted in yellow):
(function () {
"use strict";
WinJS.Binding.optimizeBindingReferences = true;
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !==
activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched.
// Initialize your application here.
} else {
// TODO: This application has been reactivated from
// suspension. Restore application state here.
}
args.setPromise(WinJS.UI.processAll());
JsCAD.onLoad();
}
};
app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended.
// Save any state that needs to persist across
// suspensions here. You might use the
// WinJS.Application.sessionState object,
// which is automatically saved and restored across
// suspension. If you need to complete an
// asynchronous operation before your application is
// suspended, call args.setPromise().
};
app.start();
})();
-
3# – And I finally edited the content of default.html using pretty much the same code I had previously in the browser version:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JsCAD</title>
<!-- WinJS references -->
<link href="//Microsoft.WinJS.1.0/css/ui-light.css"
rel="stylesheet" />
<script src="//Microsoft.WinJS.1.0/js/base.js"></script>
<script src="//Microsoft.WinJS.1.0/js/ui.js"></script>
<!-- JsCAD references -->
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/JsCAD.js"></script>
<script src="/js/default.js"></script>
</head>
<body>
<h3>Welcome to JS*CAD 1.0.0</h3>
<hr /> <!-------------------------------------->
<input type='button' onclick='JsCAD.initializeCanvas()'
value='New Drawing' />
<input type='button' onclick='JsCAD.initDrawLine()'
value='Line' />
<input type='button' onclick='JsCAD.initDrawCircle()'
value='Circle' />
Color:
<input id="colorInput"
type='text'
style="width: 55px"
onchange="JsCAD.onColorChanged()"
value="FFFFFF"/>
<input id="color" type='text' style="width: 55px" readonly/>
<hr /> <!-------------------------------------->
<canvas id="canvas" width="500" height="400">
HTML5 Canvas Not Supported...
</canvas>
</body>
</html>
Compiled that, run through the VS debugger and here is the stunning result!
![Screen shot 2013-01-14 at 11.15.12 AM_thumb[2] Screen shot 2013-01-14 at 11.15.12 AM_thumb[2]](https://adndevblog.typepad.com/.a/6a0167607c2431970b017c36180f70970b-pi)
You can get the source code of that project here:
It was so painless to achieve this that I wanted to put myself into more troubles and decided to give a further try at what Microsoft offers in its JavaScript library, fancy named WinJS. The main thing I was looking at so far is the UI part: a useful set of JavaScript controls is provided by the WinJS.UI Namespace. You will find a complete listing of them by following that link.
The way you use a Microsoft JavaScript control is by creating a containing DIV in the html document, specifying the control class name and its optional parameters:
<div id="ElementId"
data-win-control="WinJS.UI.ControlClassName"
data-win-options="{option1: value1, option2: value2, ...}">
</div>
Here are the controls I used in the enhanced version of my JsCAD app:
1. WinJS.UI.HtmlControl:
It’s a kind of frame that can contain any html document it points to, so your app can easily switch from one html page to another to replace a portion of the main html document. I used it to display the canvas and buttons of my app:
<div id="JsCADMainWnd"
data-win-control="WinJS.UI.HtmlControl"
data-win-options="{uri: '/html/JsCADMainWnd.html'}">
</div>
2. WinJS.UI.AppBar:
Pretty much every mobile app exposes options or settings and a logic place to put them is in the AppBar, a menu that appears at the bottom of the screen, either by right-clicking on desktops or swiping from the edge on touch-screens. I placed there the buttons I had previously inside the html page:
![clip_image001[4] clip_image001[4]](https://adndevblog.typepad.com/.a/6a0167607c2431970b017d40471493970c-pi)
3 . WinJS.UI.Flyout:
A dialog-like window that can get displayed anywhere on your app window, that contains html code and that can get discarded by clicking outside of it. I used it to display a color picker dialog and I used for that a control provided by Yahoo!: http://developer.yahoo.com/yui/colorpicker/
![clip_image002[4] clip_image002[4]](https://adndevblog.typepad.com/.a/6a0167607c2431970b017ee7bb54a1970d-pi)
4. WinJS.UI.Tooltip:
A tooltip control that can display any html content, not only plain text. I used it to display a link and an image when the mouse over my name in the title bar.
-
Here is the code for that enhanced version of the project:
You will find a great amount of examples for simple and advanced functionalities just there. So good luck and have fun for your future Window 8 development!