Programming Win8 with HTML5

+4

No comments posted yet

Comments

Slide 4

You are here !! The focus will be HTML/JS… Click… But do remember WInRT is helping.. And is available to provide infrastructure.. We will see a little bit of WinRT in action in some demos

Slide 1

Elad Katz senior architect @ sela Introduction to Win8 HTML5 Programming

Slide 4

Windows 8 Windows Core OS Services JavaScript (Chakra) C C++ C# VB Metro style Apps Communication & Data Application Model Devices & Printing WinRT APIs Graphics & Media XAML HTML / CSS HTML JavaScript C C++ C# VB Desktop Apps Win32 .NET / SL Internet Explorer System Services View Model Controller Core

Slide 5

Elad Katz senior architect @ sela Introduction to Win8 HTML5 Programming

Slide 6

Agenda Application anatomy JS Patterns WinJS jQuery & third parties Controls Data binding WinRT Blend 5

Slide 7

Application Anatomy

Slide 8

Hello World Disclaimer: Alpha demoes ahead. Demo

Slide 9

App on disc to running

Slide 10

HTML app platform

Slide 11

Basic Structure HTML <!DOCTYPE html> <html> <head> <title>My App</title> <link rel="stylesheet" href="my.css"> <link rel="stylesheet" href="more.css"> ... </head> <body> ... content ... <script src="my.js"></script> <script src="more.js"></script> </body> </html>

Slide 12

Basic Structure CSS button { color: #000; padding: 4px; } .page { background-color: #fff; } #my { font-weight: bold; }

Slide 13

Basic Structure JavaScript // variables and objects var a = {first : 1, second : 2}; // functions function add(x, y) { return x + y; } // function calls a.result = add(a.first, a.second);

Slide 14

Locating Elements Using “querySelector” and “querySelectorAll” HTML <a class="tab" value="1">...</a> <a class="tab" value="2">...</a> <a class="tab" value="3">...</a> CSS .tab { border-radius: 5px 5px 0 0; } JavaScript var tabs = document.querySelectorAll(".tab"); for(var i = 0; i < tabs.length; i++) { tabs[i].onclick = showTab; }

Slide 15

Listening to Events Using “on*” and “addEventListener” JavaScript // Simple registration tabs[i].onclick = showTab; // Alternate registration tabs[i].addEventListener("click", showTab, false);

Slide 16

Changing Text Using “textContent” HTML <h2 id="title">Section 1</h2> CSS #title { font-size: 20px; } JavaScript var title = document.querySelector("#title"); function setTitle(name) { title.textContent = name; }

Slide 17

Adding Elements Using “createElement” and “appendChild” HTML <body> <img src="my.png"/> </body> JavaScript function addImage() { var img = document.createElement("img"); img.src = "my.png"; document.body.appendChild(img); }

Slide 18

Hiding Elements Using “display” HTML <section id="p1" class="page">...</section> <section id="p2" class="page">...</section> CSS .page { font-size: 20px; } JavaScript function swap(oldPage, newPage) { oldPage.style.display = "none"; newPage.style.display = ""; }

Slide 19

Sending and Receiving Data Using “XMLHttpRequest” JavaScript function load(url, data, callback) { var xhr = new XMLHttpRequest(); xhr.open("GET", url, true); xhr.onload = function() { callback(xhr.responseText); } xhr.send(data); }

Slide 20

Transmitting Complex Objects Using “JSON.parse” and “JSON.stringify” JavaScript function loadJSON(url, data, callback) { var xhr = new XMLHttpRequest(); xhr.open("GET", url, true); xhr.onload = function() { callback( JSON.parse(xhr.responseText) ); } xhr.send( JSON.stringify(data) ); } loadJSON("my.json", { id : 1 }, function(response) { setTitle(response.title); });

Slide 21

Isolating JavaScript Using the Module Pattern JavaScript (function(){ var title = document.querySelector("#title"); function setTitle(name) { title.textContent = name; } setTitle("My Title"); })(); (function(){ })(); setTitle("My Title");

Slide 22

Exposing Public APIs Using the Module Pattern JavaScript var module = (function(){ var title = document.querySelector("#title"); function setTitle(name) { title.textContent = name; } return { setTitle : setTitle }; })(); module.setTitle("Custom Title"); return { setTitle : setTitle }; var module = module.setTitle("Custom Title");

Slide 23

HTML, CSS, JS HTML5 Indexed DB App Cache Web Workers Canvas SVG FormData CSS3 animations & transitions XHR FileAPI Web Sockets Geolocation PostMessage

Slide 24

Single HTML5 DOCTYPE Standards web, plug-in free Single window Access to Windows Runtime

Slide 26

Application lifetime

Slide 27

sessionState in Windows Library for JavaScript (WinJS) can help. Use Windows.Storage.ApplicationData to save app state.

Slide 28

RSS reader #1 Xhr, WinJS, templating & css Demo

Slide 29

WinJS

Slide 30

Async … is everywhere in JavaScript … with different models Callback parameter setTimeout(function () { … }, 500); Attached event handler var img = document.querySelector("img .loading"); img.addEventHandler("load", function () { imageLoaded(); }, false); img.src = "http://www.example.org/someImage.jpg"; Is everywhere in WinRT – you can’t block! Major JS libraries have an established async pattern

Slide 31

Promises Object that is a promise for a value later Implemented by jQuery, dojo, node.js, etc. Hook up to completion with then() method then(completion, error, progress) then() returns another promise Implementation in base.js: WinJS.Promise Common.js promises/A spec http://wiki.commonjs.org/wiki/Promises/A

Slide 32

Example: Completion Handling WinJS.xhr({ url:"http://www.example.org/somedata.json" }).then(function (response) { updateDisplay( JSON.parse(response.responseText)); });

Slide 33

Example: Error Handling WinJS.xhr({ url:"http://www.example.org/somedata.json" }).then(function (response) { updateDisplay( JSON.parse(response.responseText)); }, function (ex) { reportXhrError(ex); });

Slide 34

WinJS WinJS is the main JavaScript library for Windows8, which consists of several files. It adds support for: Controls DataBinding Animations Make your apps look and feel great Matches the Windows Metro design style Controls for common user experiences Designed for touch as well as traditional input Scales across form factors

Slide 35

WinJS WinJS in itself is regular JavaScript, but it has dependencies on WinRT and MS specific features Porting browser applications to Metro applications is pretty straight forward Creating cross platform application is not possible, but the code-base sharing is very good.

Slide 36

Third party libraries Metro applications are fully compatible with libraries such as jQuery and KnockoutJS Many best practices that are in use in winJS comes from these libraries. Use jQuery if you’re planning on making it cross-platform later on. Otherwise it’s probably not worth it. KnockoutJS is more sophisticated than WinJS at this stage, but this will probably change in future builds

Slide 37

WinJS Controls

Slide 38

I thought HTML already had controls? HTML has elements WinJS Controls are: JavaScript objects attached to an element Reusable chunks of behavior attached to the DOM Declarative or programmatic Toolable

Slide 39

In-box controls for Metro style apps App Bar Rating Button List Box Hyperlink Slider Checkbox Radio Button Toggle Switch Tooltip Context Menu List View Combo Box Progress Bar Text Box Password Progress Ring Flyout Grid View Flip View Scroll Bar Panning Indicator

Slide 40

WinJS Controls Demo

Slide 41

Control Library

Slide 42

Control Library

Slide 43

Declarative and programmatic control creation <div id="target"></div> <div data-win-control="WinJS.UI.TimePicker" data-win-options="{minuteIncrement: 15}"></div> WinJS.UI.processAll(); var target = document.getElementById("target"); var timePicker = new WinJS.UI.TimePicker(target, { minuteIncrement: 15});

Slide 44

It’s not a Microsoft UI stack without data binding Data binding

Slide 45

What it does One way data binding Object -> UI (or any function) Asynchronous notifications Programmatic binding Declarative binding

Slide 46

Data binding demo

Slide 47

Template binding <div data-win-control="WinJS.Binding.Template"> <div class="feedItem"> <h2 data-win-bind="textContent: title"></h2> <img data-win-bind="src: thumbnailUrl"></img> <p data-win-bind="innerHTML: summary"> </div> </div>

Slide 48

Declarative binding <h1 id="title" data-win-bind="textContent: title; style.background: color"/> var data = WinJS.Binding.as({ title: "This is my title", color: "#80FF80" }); WinJS.Binding.process(document.getElementById("title"), data); data.title = "new title here";

Slide 49

Programmatic binding var data = WinJS.Binding.as({ a: 10, b: 50}); data.bind("a", function (newValue, oldValue) { var target = document.getElementById("target"); target.textContent = newValue; }); data.a = 50;

Slide 50

WinRT

Slide 51

WinRT The namespace windows is reserved to WinRT WinRT let us have direct access to the new windows API Useful mostly for windows contracts, or in scenarios where we don’t care about other platforms

Slide 52

WinRT – Harnessing the power of Windows Search Contract Share Contract Demo

Slide 53

Navigation

Slide 54

Multipage vs. single page navigation Multipage navigation Default behavior of browser Navigation loads a new page Drops script context Have to serialize state that gets passed across pages Single page navigation One page as far as browser is concerned DOM elements changed programmatically Keeps script context and state for app lifetime

Slide 55

WinJS single page navigation Metro style apps use single page navigation State management becomes easy It’s difficult to design all of your app in a single HTML file

Slide 56

Separate logical pages with fragments We want separate HTML files Easier to design Easier to maintain Fragments Separate files, plus a loader that will bring them in In ui.js: WinJS.UI.Fragments namespace

Slide 57

Navigation & fragments demo

Slide 58

Example: fragment loader var targetDiv = document.getElementById("targetDiv"); WinJS.UI.Fragments.cloneTo("/html/feeds.html", targetDiv). then(function () { return WinJS.UI.processAll(targetDiv); }).then(function () { document.body.focus(); });

Slide 59

Navigation & fragments function navigated(e) { WinJS.UI.Fragments.clone(e.detail.location, e.detail.state) .then(function (frag) { var host = document.getElementById('contentHost'); host.innerHTML = ''; host.appendChild(frag); }); } WinJS.Navigation.addEventListener("navigated", navigated); WinJS.Navigation.navigate("/html/home.html");

Slide 60

Blend 5 The excellent XAML authoring tool is now for HTML5 as well. HTML designers in the world of jQuery plugins become less and less relevant – it’s a very hard task to style a jQueryUI control, and it’s done manually. Blend5 has the IE10 engine inside, and can run our JS to allow us perfect design-time experience

Slide 61

VS11 & Blend 5 tooling HTML can be stronger than compiled languages Demo

Slide 62

Summary Win8 HTML5 applications are first and foremost “Single page” HTML applications Porting from web apps to win8 is pretty straightforward. Not so much the other way around WinRT is purely for Windows WinJS is de-facto purely for Windows Large code base can be shared.

Slide 63

Summary Win8 HTML application uses best practices for: Control library Databinding and templating Single-Page practices WinRT integration is as simple as it is from other languages

Slide 64

Elad Katz Twitter: @eladkt http://blogs.microsoft.co.il/blogs/eladkatz thank you

URL:
More by this User
Most Viewed