by Jeremy Foster
26. March 2012 11:10
If you create a blank JavaScript application in Windows 8, you get just that – a blank application. You don’t get page references to the WinJS library, you don’t get a grid or a list or any other navigation structure, and you don’t get the navigation code to implement the Microsoft recommended navigation method – single page navigation.
If you want to implement navigation, you can obviously just create a new application from the Navigation Appliction template (built-in), but if you’re like me, it’s nice to add in all of a given piece of functionality from scratch for understanding’s sake.
Follow these steps to add navigation to a blank application. And I encourage you to actually type all of the code instead of just copying and pasting. If you type it, your brain will pick it up better.
- In Visual Studio 11, create a new Blank Application using JavaScript
- Add an html folder at the root of your project
- Right click on the html folder and Add | New Item…
- Choose Page Control and call your new item page1.html
Note: You’ll see that you got your new .html file as well as a .css and a .js - Drag the .css file into the css folder and the .js file into the js folder
- Modify the css link reference and js script reference on your html file changing “page1.js” to “/js/page1.js” and “/css/page1.css”
- Grab a copy of navigator.js from any other sample app or template app. You can create a throw-away project from the Navigator Application to get a copy of this file if necessary.
- Modify the navigator.js file changing the name of the namespace to the project name of your application. For instance…
WinJS.Namespace.define("YourProjectName", { ...
- Add a reference to the navigator.js file into the default.html file
- Add a PageControlNavigator to the body of the default.html file like the following…
<div id="contenthost"
data-win-control="Application2.PageControlNavigator"
data-win-options="{ home: '/html/page1.html' }"></div>
Note: the data-win-control makes this an official WinJS control. The data-win-options “home” property tells this control which page to start with and which page to return to when the user elects through the navigator to go home.
Learning a new environment or framework or object model always takes some time and exposure, so take some time, exposure yourself to tasks like this, and have fun.
fdbc7e9a-33e6-498b-b5be-b4ae0f7d314f|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
Tags:
Windows 8