HTML and CSS Reference
In-Depth Information
FIGURE 12-9
The Print-n-Go application.
registering the
Print
contract
Most of the magic required to print content in Windows 8 happens during the initial phase of an
application. In fact, after registering the
Print
contract during initialization, you're nearly all set. Note
that application initialization is only the preferred place where you could place the
Print
contract; you
could do that later in the application. All that really matters is that you must have the
Print
contract in
place before the user can invoke any printing functionality.
As usual for the exercises in this topic, you have an
init
method on the main application object.
Here's an implementation of the method that registers the
Print
contract:
printerApp.init = function () {
var printManager = Windows.Graphics.Printing.PrintManager;
var printView = printManager.getForCurrentView();
printView.onprinttaskrequested = function (eventArgs) {
var printTask = eventArgs.request.createPrintTask("Print-n-Go", function
(args) {
args.setSource(MSApp.getHtmlPrintDocumentSource(document));
printTask.oncompleted = onPrintTaskCompleted;
});
};
function onPrintTaskCompleted(eventArgs) {
if (eventArgs.completion === Windows.Graphics.Printing.PrintTaskCompletion.
failed) {
printerApp.alert("Failed to print.");
}
}
}
Registering the
Print
contract essentially means invoking the
getForCurrentView
method on the
PrintManager
system object. The returned view must be further configured with a handler for the
requested print task event. In other words, anytime a user requests a print task through the charms,
or anytime an app requests print services programmatically, the application must create a print task.
Search WWH ::
Custom Search