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.

For example, the Music app that comes with Windows 8 allows you create a new playlist when you’re<br />

viewing tracks of some album. The following commands appear on the app bar (left), and when you<br />

select New Playlist, a flyout appears (middle) requesting the name, after which the flyout appears on the<br />

app bar (right):<br />

The playlist then appears within the app as another album. In other words, though playlists might be<br />

saved in discrete files, they aren’t necessarily presented that way to the user, and the API reflects that<br />

usage pattern.<br />

Loading a playlist uses the Playlist.loadAsync method given a StorageFile for the playlist. This<br />

might be a StorageFile obtained from a file picker or from the enumeration of the app’s private playlist<br />

folder. Scenario 2 of the Playlist sample (display.js) demonstrates the former, where it then goes through<br />

each file and requests their music properties:<br />

function displayPlaylist() {<br />

var picker = new Windows.Storage.Pickers.FileOpenPicker();<br />

picker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.musicLibrary;<br />

picker.fileTypeFilter.replaceAll(SdkSample.playlistExtensions);<br />

var promiseCount = 0;<br />

picker.pickSingleFileAsync()<br />

.then(function (item) {<br />

if (item) {<br />

return Windows.Media.Playlists.Playlist.loadAsync(item);<br />

}<br />

return WinJS.Promise.wrapError("No file picked.");<br />

})<br />

.then(function (playlist) {<br />

SdkSample.playlist = playlist;<br />

var promises = {};<br />

// Request music properties for each file in the playlist.<br />

playlist.files.forEach(function (file) {<br />

promises[promiseCount++] = file.properties.getMusicPropertiesAsync();<br />

});<br />

// Print the music properties for each file. Due to the asynchronous<br />

// nature of the call to retrieve music properties, the data may appear<br />

// in an order different than the one specified in the original playlist.<br />

432

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

Saved successfully!

Ooh no, something went wrong!