23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

scenario oriented on the music library that lets you enter in an AQS string directly. However, this isn’t<br />

always what you’ll be using in an app, so I wanted to show many other variations.<br />

Queries always start with a StorageFolder, whose createFileQuery[WithOptions],<br />

createFolderQuery[WithOptions], and createItemQuery[WithOptions] methods (6 total) provide for<br />

enumerating files, folders, or both, within whatever folder the StorageFolder is attached to. The<br />

simplest queries are created with the base StorageFolder.create* methods and no parameters:<br />

folder.createFileQuery();<br />

folder.createFolderQuery();<br />

folder.createItemQuery();<br />

The first two methods here are actually just shortcuts for the one-parameter variants with the same<br />

names, where that parameter is a value from the Windows.Storage.Search.CommonFileQuery or<br />

CommonFolderQuery enumerations. These shortcut versions just use the defaultQuery value for a simple<br />

alphabetical, shallow enumeration of the folder contents. createItemQuery, for its part, has only this one<br />

form.<br />

Creating a query itself doesn’t actually enumerate anything until you ask it to through an async<br />

method: for file queries the method is getFilesAsync, for folders it’s getFoldersAsync, and for items it’s<br />

getItemsAsync. (See a pattern here?) So, in Scenario 2 of the FileQuery example, I have these three<br />

functions attached to buttons:<br />

function fileQuery() {<br />

var query = picturesLibrary.createFileQuery();<br />

SdkSample.showResults(query.getFilesAsync());<br />

}<br />

function folderQuery() {<br />

var query = picturesLibrary.createFolderQuery();<br />

SdkSample.showResults(query.getFoldersAsync());<br />

}<br />

function itemQuery() {<br />

var query = picturesLibrary.createItemQuery();<br />

SdkSample.showResults(query.getItemsAsync());<br />

}<br />

where the SdkSample.showResults function in js/default.js just creates a listing of the items in the<br />

collection. Running this sample, you’ll see a list of those files and/or folders in your Pictures library.<br />

Tip The actual object types returned by these create*Query APIs are StorageFileQueryResult,<br />

StorageFolderQueryResult, and StorageItemQueryResult, all in the Windows.Storage.Search<br />

namespace. These all provide some additional properties like folder, methods like<br />

findStartIndexAsync and getItemCountAsync, and events like optionschanged and<br />

contentschanged (both WinRT events). The latter event especially is something you can use to monitor<br />

changes to the file system that affect query results.<br />

355

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

Saved successfully!

Ooh no, something went wrong!