Programming Windows Store Apps with JavaScript and WinRT
Rainer Stropek
software architects gmbh
Rainer Stropek software architects gmbh Introduction software - - PowerPoint PPT Presentation
Programming Windows Store Apps with JavaScript and WinRT Rainer Stropek software architects gmbh Introduction software architects gmbh Rainer Stropek Developer, Speaker, Trainer MVP for Windows Azure rainer@timecockpit.com
software architects gmbh
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 2
http://www.timecockpit.com
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 3
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 4
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 5
Goals…
from the web for developing Windows applications
class citizens for developing Windows Store Apps
HTML + JavaScript Prerequisites…
know from IE 10: HTML5 + CSS3 + JavaScript
(WinRT)
Plugin-Free, etc.
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 6
Non-Goals of the Session:
Tip: Download slides and check hidden slides for code snippets
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 7
portrait/landscape, snapped, etc.; read more)
read more)
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 8
(functio (function ( ) n ( ) { // create variable // var name = "Andi und Rainer"; // create method // var sayHello = function ( ) { // alert(name); // }; })( ); })( ); // calling this method.
It is plain old JavaScript, also for Windows Store Apps!
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 9
Background information in MSDN
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 10
Reference to WinJS Folder for CSS-Dateien Folder for images Folder for JavaScript files HTML files Application Manifest
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 11
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 12
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 13
<!-- add a WinJS control in HTML --> <div class="itemlist" data data-wi win-co cont ntro rol=" ="Wi WinJ nJS.U .UI. I.Li List stVie iew" w" […]></div> // find control in DOM tree var lv = document.body.querySelector(".itemlist"); // fill WinJS list (=data source for ListView) var items = new Win inJS JS.Bi Bind ndin ing.L .Lis ist(); for (var i = 1000; i < 1500; i++) { items.push("Hello World!"); } Win inJS JS.UI UI.p .pro roces essA sAll ll() ().th then en(function() { lv.win inCo Cont ntro rol.i .ite temD mDat ataSo Sour urce ce = = it item ems. s.dat ataS aSou
ce; }
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 14
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 15
<div class="splitpage"> <div class="itemtemplate" data data-win-control="WinJS.Binding.Template"> <div><img src="#" data data-win win-bi bind nd=" ="sr src: c: Im Imag ageUri ri; ; al alt: : Ti Titl tle" e" style="width: 100px" /></div> <div class="item-info"><h3 class="item-title win-type-ellipsis" da data ta-win win-bi bind nd=" ="te textC tCon
nt: : Ti Title le"></h3></div> </div> <header aria-label="Header content" role="banner"><h1>Garden Blog</h1></header> <section class="itemlistsection" aria-label="List section"> <div class="itemlist" aria-label="List of this group's items" data-win-control="WinJS.UI.ListView" data-win-options="{ selectionMode: 'single', tapBehavior: 'directSelect' }"> </div> </section> <section class="articlesection" aria-atomic="true" aria-label="Item detail" aria-live="assertive"> <header class="header"> <div class="text"><h2 data data-wi win-bi bind nd=" ="tex extC tConte tent nt: : Tit itle le"></h2></div> <div> <img class="article-image" src="#" data data-win win-bi bind nd=" ="src rc: : Im Imag ageUr Uri; i; alt lt: : Ti Title le" style="width: 150px; max-height: 150px" /> </div> </header> <article class="article-content" data data-win win-bind nd=" ="in inner erHT HTML ML: : Tex ext" t"></article> </section> </div>
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 16
function onselectionchanged(eventObject) { var listView = document.body.querySelector(".itemlist").winControl; // By default, the selection is restriced to a single item. listView.selection.getItems().then(function (items) { if (items.length > 0 && items[0]) { var details = document.querySelector(".articlesection"); WinJS.Bi WinJS.Binding. nding.processA processAll(det ll(details, it ails, items[0] ems[0].data); .data); details.scrollTop = 0; } }) } Root node for data binding Data Context for data binding
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 17
WinJS.UI.setOptions(lv.winControl, { itemTemp itemTemplate: late: document document.body. .body.querySel querySelector( ector(".itemte ".itemtemplate mplate"), "),
ionchanged: on nged: onselect selectionchang ionchanged.bin ed.bind(this), d(this), itemDataSource: items.dataSource, layout: new WinJS.UI.ListLayout() }); lv.winCo lv.winControl. ntrol.selectio selection.set( n.set(0); 0);
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 18
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 19
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 20
function refreshBlogList() { WinJS.xhr WinJS.xhr({ url: "http://localhost:2220/ServiceBlog.svc/Blog/" }).done(function (feedResponse) { // convert feed to list of bindable objects var feed = JSON.parse JSON.parse(feedResponse.responseText); var items = new WinJS.Binding.List(feed) new WinJS.Binding.List(feed); // set properties of list view (including data source for binding) var lv = document.body.querySelector(".itemlist"); WinJS.UI.setOptions(lv.winControl, { itemTemplate: document.body.querySelector(".itemtemplate"),
itemDataSource: items.dataSource, layout: new WinJS.UI.ListLayout() }); // automatically select first item lv.winControl.selection.set(0); }); }
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 21
app.onactivated = function (eventObject) { // lookup list view in DOM var lv = document.body.querySelector(".itemlist"); WinJS.UI.processAll().then(function () { refreshB refreshBlogLis logList(); t(); }); }
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 22
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 23
Windows Azure
Metro-Style App
Webrole (REST Service) Azure Blob Storage (Bilder)
HTTP GET JSON (Blog Entries) HTTP GET Blobs (Bild)
SQL Azure (Blog Content
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 24
Windows Azure
Metro-Style App
Webrole (REST Service) Azure Blob Storage (Bilder)
HTTP GET Shared Access Signature HTTP POST (Bild) Generate Shared Access Signature (SAS)
SQL Azure (Blog Content
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 25
Link with signature
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 26
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 27
<div id="appbar" data ta-wi win-co contr trol
="Wi WinJS JS.U .UI. I.App ppBa Bar"> <button data data-wi win-co cont ntrol
="Wi WinJS JS.U .UI. I.Ap AppBa BarC rCom
and" data-win-options="{id:'cmd', label:'Create Entry', icon:'placeholder'}"> </button> <button data-win-control="WinJS.UI.AppBarCommand" data-win-options="{id:'refresh', label:'Refresh', icon:'placeholder'}"> </button> </div> // add button handler for appbar button var cmdButton = document.getElementById("cmd"); cmd mdBu Butto ton. n.on
lick ck = = on
Post stBl BlogE gEnt ntry ryCl Click ck; var refreshButton = document.getElementById("refresh"); refreshButton.onclick = refreshBlogList;
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 28
function onPostBlogEntryClick() { // let user take a picture. Note that we use WinRT classes here. var capture = Windows. Windows.Media. Media.Capture Capture; var cameraCaptureUI = new capture.CameraCaptureUI(); cameraCaptureUI.ca capt ptureFi ureFile leAsy sync nc(capture.CameraCaptureUIMode.photo).then( function (capturedItem) { if (capturedItem) { […] } }); }
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 29
if (capturedItem) { var rootDiv = document.body.querySelector(".itemlist"); var busy = getBusyOverlay(rootDiv, { color: 'gray', opacity: 0.25 }, { type: 'oval', color: '#FFFFFF' }); WinJS.xhr({ url: "http://localhost:2220/ServiceBlog.svc/Blog/GetSasForImage/" + imageName }) .done(function (result) { var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader(); uploader.method = "PUT"; uploader.setSourceFile(capturedItem); var uri = new Windows.Foundation.Uri(JSON.parse(result.responseText)); var upload = uploader.createUpload(uri); upload.startAsync().done(function (result) { WinJS.xhr({ url: "http://localhost:2220/ServiceBlog.svc/Blog/AddBlog", data: JSON.stringify(newEntry), type: "POST", headers: { "Content-Type": "text/JSON" } }).done(function () { busy.remove(); refreshBlogList(); }); }, function (err) { busy.remove(); }); }); }
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 30
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 31
var dtm = Wind ndow
s.Ap Appli lica catio ionM nMode del. l.Da Data taTra rans nsfer er.D .Dat ataT aTran ansf sferM rMan anage ger. r.ge getF tForC rCur urren entV tVie iew(); dtm.addEventListener("datarequested", function (e) { // capture request member var request = e.request; // get selected item from list view var lv = document.body.querySelector(".itemlist").winControl; lv.selection.getItems().then(function (items) { if (items.length > 0) { var item = items[0].data; // set request title and description re requ ques est.d .dat ata.p .pro rope pert rties es.t .titl tle e = i ite tem. m.Ti Title le; re requ ques est.d .dat ata.p .pro rope pert rties es.d .desc scri ripti tion
= " "Gar arde den B Blo log" g"; // set text content var recipe = item.Text; re requ ques est.d .dat ata.s .set etTe Text xt(re reci cipe) e); // set image content // note that we use deferral-API here. // (only do this if your operation is quite fast; 200ms time limit or so) var uri = new Windows.Foundation.Uri(item.ImageUri); var reference = Windows.Storage.Streams.RandomAccessStreamReference.createFromUri(uri); request.data.properties.thumbnail = reference; va var r de defer erra ral = = r req eque uest. t.ge getDe Defe ferra ral( l(); ); re requ ques est.d .dat ata.s .set etBi Bitm tmap( p(re refer eren ence) e); de defe ferr rral. l.co compl plet ete( e(); ); } }); });
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 32
software architects gmbh
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 34
Herbstcampus 2012 – Windows Store Apps with HTML and JavaScript 35