HTML and CSS Reference
In-Depth Information
It is now about time you replace the basic message box with the code that actually creates the file.
Saving data to a file
In Microsoft Windows 8, the File Save Picker component seeks to be more helpful than one might
expect. It doesn't simply return you an object that describes the file you intend to create; it returns
you a true file object. In other words, the object you get from the picker refers to a file that has
already been created for you. The file is empty, but it exists already. Subsequently, all you have to do
is write some text to it.
You open the todolist.js file and add the following code to the TodoList.invokeSavePicker method to
deal with the File Save Picker:
savePicker.pickSaveFileAsync().then(function (file) {
if (file) {
// The file ALREADY exists; the file picker CREATED it with 0 bytes
Windows.Storage.FileIO
.writeTextAsync(file, "Some data")
.done(function () {
TodoList.alert("Data successfully saved");
},
function (error) {
TodoList.alert("Unable to save data. Sorry about that!");
}
);
}
});
Admittedly, the syntax of the I/O objects in Windows 8 is a bit convoluted, but not really scary.
The basic object for I/O manipulations is Windows.Storage.FileIO . This object exposes a method
called writeTextAsync ; it takes a file object and some text and simply writes the text to the file. In the
example above, you are saving the text “Some data” to the picked file.
In Windows Store applications, most system operations are performed in an asynchronous
way. This means that the instruction that follows the asynchronous call executes immediately
without waiting for the other instruction to complete. While asynchronous calls ensure the highest
responsiveness of the application's user interface, they make underlying code harder to read and
write.
In particular, to express a sequential semantic where two or more actions occur one after the
completion of the previous, you need to resort to a “fluent” syntax, as shown below:
Windows.Storage.FileIO
.writeTextAsync( ... )
.done( ok, error )
Search WWH ::




Custom Search