Escolar Documentos
Profissional Documentos
Cultura Documentos
The Goal
Get started creating Metro style applications
Using Javascript, HTML5 and CSS3
Introduce the basic concepts Investigate some of the Windows Runtime Controls Use Asynchronous APIs
Assumptions
Basic knowledge of Web technologies:
HTML, JavaScript, CSS
Basic knowledge of .NET development/tools Visual Studio, Blend Basic knowledge of Metro style apps and
Windows 8
Applications
mode available as well .NET Framework and CLR appear on both sides (blue and green)
VB and C# apps still require .NET Framework 4.5, even when creating Metro apps
Limited subset for Metro apps
Many changes in APIs to support Metro style apps and asynchronous behavior
All Metro APIs are asynchronous
DEMO 1
Hello, World using HTML5/CSS/JavaScript
us/library/windows/apps/br211385.aspx
DEMO 2
Create markup
Modify Styles
Replace style information in default.css Note class and id-based styles
DEMO 3
Add style information
Main Function
Code in self-executing anonymous function
Recommended JavaScript practice Avoids polluting the global namespace
Ready to Fire!
Must indicate to the application that youre ready to receive events Can appear anywhere in the main function
As long as it executes after initialization
(function() { // Code removed here app.start(); }) ();
Downloading Data
Need some means of making an HTTP request In WinJS, xhr function provides capability
xhr == XMLHttpRequest xhr has a number of options
Which HTTP verb to use (GET is default) Which HTTP headers to include (none by default) Which URL to use
Passing Information
Supply a JSON object containing fields corresponding to the parameters:
type url (required) user password headers data
Synchronous or Asynchronous?
XMLHttpRequest object allows developer to select synchronous or asynchronous result
xhr forces asynchronous
UI isnt blocked while awaiting result
Return Value
Asynchronous function in WinJS return object called a promise (called deferred in some APIs) Promise must handle successful completion
And error conditions
Promise.then Method
Understand that the then method indicates functions to call on success and failure Call to xhr returns the Promise object immediately
Set the functions to call on success and failure This example ignores progress
DEMO 4
Add code to set up downloads Discuss
What Happened?
Once the xhr call completes
Code calls processPosts function Passes request content as parameter
Contains requested RSS data
Whats Next?
Modify application
Add support for Windows Runtime methods
Easier to use Windows.Web.Syndication namespace rather
In addition to WinJS, Metro style apps can access classes in Windows Runtime
Windows.Web.Syndication namespace supports RSS
feeds:
DEMO 5
Replace code, using SyndicationClient
Provide styles and behavior for the data-wincontrol attribute (required for templates) HTML5 defines set of data-* attributes for associating app-specific information with element
Do not forget:
In order to cause data-win-control attributes to take
Binding Data
WinJS.Binding.Template class connects properties of elements with data Looks for data-win-bind attribute in elements
Format looks like this:
data-win-bind="innerText: title"
Part before ":" indicates property of the element Part after ":" indicates JavaScript property to bind
DEMO 6
Add references Add template Modify code Run
Things to Note
Calling document.getElementById retrieves reference to HTML element
Provided property with same name as element
Works in IE only, but fine for Metro apps
WinRT control:
Things to Note
For each item in the RSS feed, the code creates JSON object named post containing the information:
var item = feed.items[i]; var post = { title: item.title.text, date: item.publishedDate, content: item.summary.text, };
Things to Note
The template provides render method
Stamps out instance of the template for each item you
supply
Current Status
Have used WinJS and Windows Runtime to make the code clearer Have used templates and simple binding to make it easier to modify the layout Wouldn't it be nicer to view list of topics
And then click on one to see its contents? ListView control makes that easier Also handles repetitive binding
Adding a ListView
In addition to template control and intrinsic HTML controls
WinJS provides several other built-in controls ListView control allows arrangement of data into list or
grid
ListView Markup
data-win-options attribute specifies parameters to pass to WinJS.UI.ListView constructor
Attribute value is JSON object Some parameters are JSON objects (layout)
<div id="posts" data-win-control="WinJS.UI.ListView" data-win-options="{itemTemplate: template, layout: {type: WinJS.UI.ListLayout}}"></div>
DEMO 7
Modify markup Look at documentation for WinJS.UI.ListView Modify processPosts() Note dataSource property of ListView
What's Next?
Current demo implementation shows only blog entry titles
Need user interface to display content of selected
right
Modify Layout
Layout is defined in CSS Start by modifying body, #posts, and #content elements
So layout is appropriate for two-column display
DEMO 8
Modify CSS
Things to Note
display: -ms-grid; -ms-grid-rows: auto 1fr; -ms-grid-columns: 1fr 1fr;
Things to Note
#posts and #content element descriptions indicate row and column locations (1-based):
#posts { width: 99%; height: 100%; overflow: auto; -ms-grid-row: 2; -ms-grid-column: 1; } #content { width: 95%; height: 100%; overflow-y: auto; margin-right: 64px; -ms-grid-row: 2; -ms-grid-column: 2; }
Modify Markup
Once CSS completed
Modify markup Re-adding template for content
Things to Note
Modified markup for ListView
<div id="posts" data-win-control="WinJS.UI.ListView" data-win-options="{itemTemplate: template, layout: {type: WinJS.UI.ListLayout}, selectionMode: 'single', onselectionchanged: selectionChanged}"> </div>
selectionMode: allow single selection only onselectionchanged: provide event handler declaratively
Things to Note
Markup for content template:
<div id="contentTemplate" data-win-control="WinJS.Binding.Template"> <div data-win-bind="innerText: title" class="postTitle"></div> <div data-win-bind="innerText: date" class="postDate"></div> <div data-win-bind="innerHTML: content" class="postContent"></div> </div>
CSS classes all still exist in default.css Three <div> elements for title, date, content
Bound to innerText/HTML properties of element
DEMO 9
Add new markup
DEMO 10
Add event handler Run Fix namespace Run
Things to Note
Can define namespace and specify public names for members:
WinJS.Namespace.define('rssReader', { selectionChanged: selectionChanged });
DEMO
Edit project in Expression Blend
HTML Templates
HTML Templates
Blank Application
Minimal application using Metro style frameworks
Grid Application
Multi-page project navigating multiple layers of
HTML Templates
Navigation Application
Minimal application using Windows Metro style
Split Application
A more complex project supporting navigation in a
master list of items while viewing their details on the same page
DEMO
HTML Templates
Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!