23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

One thing we can see comparing with this the equivalent JavaScript code is that the C# await<br />

keyword very much simplifies dealing with asynchronous methods—making them appear like they’re<br />

synchronous. This is one potential advantage to writing code in a component! The other important<br />

detail is to note the using statements around the streams. Streams, among other types, are disposable<br />

(they have an IDisposable interface) and must be cleaned up after use or else files will remain open and<br />

you’ll see access denied exceptions or other strange behaviors. The using statement encapsulates that<br />

cleanup logic for you.<br />

In any case, with this method now we need only a few lines of JavaScript to do the job:<br />

PixelCruncherCS.Grayscale.convertGrayscaleFileAsync(imageFile).done(function (tempFile) {<br />

if (tempFile != null) {<br />

document.getElementById("image2").src = "ms-appdata:///temp/" + tempFile.name;<br />

}<br />

});<br />

The line with the URI could be replaced with these as well:<br />

var uri = URL.createObjectURL(tempFile, { oneTimeOnly: true });<br />

document.getElementById("image2").src = uri;<br />

Running tests with this form of image conversion, the app shows a much better response, so much so<br />

that the progress ring that’s normally shown while the operation is running doesn’t even appear!<br />

All this illustrates the final point of this whole exercise. If you’re looking for optimizations, think<br />

beyond just the most computationally intensive operations, especially if it involves moving lots of data<br />

around. As we’ve seen here, challenging our first assumptions can lead to a much better overall solution.<br />

Projections into JavaScript<br />

Already in this chapter we’ve seen some of the specific ways that a WinRT component is projected into<br />

JavaScript. In this section I wanted to offer a fuller summary of how this world of WinRT looks from<br />

JavaScript’s point of view.<br />

Let’s start with naming. We’ve seen that a JavaScript app project must add a component as a<br />

reference, at which point the component’s namespace becomes inherently available in JavaScript; no<br />

other declarations are required. The namespace and the classes in the component just come straight<br />

through into JavaScript. What does change, however, are the names of methods, properties, and events.<br />

Although namespaces and class names are projected as they exist in the component, method and<br />

property names (including members of struct and enum) are converted to camel casing: TestMethod<br />

and TestProperty in the component become testMethod and testProperty in JavaScript. This casing<br />

change can have some occasional odd side effects, as when the component’s name starts with two<br />

capital letters such as UIProperty, which will come through as uIProperty.<br />

746

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!