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.

function pickSinglePhoto() {<br />

// Verify that we are currently not snapped, or that we can unsnap to open the picker<br />

var currentState = Windows.UI.ViewManagement.ApplicationView.value;<br />

if (currentState === Windows.UI.ViewManagement.ApplicationViewState.snapped &&<br />

!Windows.UI.ViewManagement.ApplicationView.tryUnsnap()) {<br />

// Fail silently if we can't unsnap<br />

return;<br />

}<br />

// Create the picker object and set options<br />

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

openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;<br />

openPicker.suggestedStartLocation =<br />

Windows.Storage.Pickers.PickerLocationId.picturesLibrary;<br />

// Users expect to have a filtered view of their folders depending on the scenario.<br />

// For example, when choosing a documents folder, restrict the filetypes to documents<br />

// for your application.<br />

openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);<br />

}<br />

// Open the picker for the user to pick a file<br />

openPicker.pickSingleFileAsync().done(function (file) {<br />

if (file) {<br />

// Application now has read/write access to the picked file<br />

} else {<br />

// The picker was dismissed with no selected file<br />

}<br />

});<br />

As you can see, you should not try to invoke the File Picker when in snapped view; this will, like the<br />

Settings Pane, cause an exception. You can check for such a condition ahead of time, as shown here, or<br />

you can add an error handler within the done at the end. 44 In any case, to invoke the picker we create<br />

an instance of Windows.Storage.Pickers.FileOpenPicker, configure it, and then call its<br />

pick-SingleFileAsync method. The result of pickSingleFileAsync is the file argument given to the<br />

completed handler, which will be either a StorageFile object for the selected file or null if the user<br />

canceled. This is why you must always check that the picker’s result is not null.<br />

With the configuration, here we’re setting the picker’s viewMode to thumbnail (from the enumeration<br />

Windows.Storage.Pickers.PickerViewMode), resulting in the view of Figure 8-7. The other possibility<br />

here is list, which gives a view like Figure 8-10.<br />

We also set the suggestedStartLocation to the picturesLibrary, which is a value from the<br />

Windows.Storage.Pickers.PickerLocationId enumeration; other possibilities are documentsLibrary,<br />

computerFolder, desktop, downloads, homeGroup, musicLibrary, and videosLibrary, basically all the other<br />

locations you see in Figure 8-8. Note that using these locations does not require you to declare<br />

capabilities in your manifest because by using the picker, the user is giving consent for you to access<br />

44 The sample, it should be noted, uses then instead of done on that last async call; while then works, it should actually be<br />

done especially if you’re going to handle exceptions there.<br />

349

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

Saved successfully!

Ooh no, something went wrong!