/// <summary> /// Standard Cancel handler for a background task /// </summary> /// <param name="sender"></param> /// <param name="reason"></param> private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { // // Indicates that the background task is canceled. // UtilitiesLibrary.SendToastNotification("Background Print Workflow Task " + sender.Task.Name + " received a Cancel Request, reason " + reason.ToString() + ", instance " + sender.InstanceId.ToString(), null); Task.Delay(60000); }
/// <summary> /// Handle the Print Task Submitted Event using the XPS Object Model /// Raised after the UI part of the app has, optionally, had its OnSetupRequested and OnXpsDataAvailable called. /// This event provides all data already provided on the OnSetupRequested event. It additionally provides /// a reference to the PrintWorkflowObjectModelTargetPackage interface enabling manipulation of the output via the XPS object model. /// </summary> /// <param name="sessionManager">Session manager</param> /// <param name="printTaskSubmittedArgs">Has the Controller, Content, and Configuration</param> private void OnXpsOMPrintSubmitted(PrintWorkflowBackgroundSession sessionManager, PrintWorkflowSubmittedEventArgs printTaskSubmittedArgs) { // Take out a deferral whilst the OM generation happens Deferral submittedDeferral = printTaskSubmittedArgs.GetDeferral(); // Send a toast UtilitiesLibrary.SendToastNotification("Background Print Workflow printing started for " + printTaskSubmittedArgs.Operation.Configuration.JobTitle, null); // Get the source XPS OM content PrintWorkflowObjectModelSourceFileContent xpsOMSourceContent = printTaskSubmittedArgs.Operation.XpsContent.GetSourceSpoolDataAsXpsObjectModel(); // Send the print ticket if available to get the Target PrintWorkflowTarget target = printTaskSubmittedArgs.GetTarget(null); // Get the Target Package PrintWorkflowObjectModelTargetPackage targetPackage = target.TargetAsXpsObjectModelPackage; // Create a Windows Runtime XPS Receiver callback object via the XpsOMRuntimeComponent, which implements // the IPrintWorkflowXpsReceiver interface and PrintWorkflowObjectModelSourceFileContentNative. It is in the // IPrintWorkflowXpsReceiver implementation that the output is actually modified var xpsReceiver = new PrintWorkflowObjectModelSourceFileContentNative(xpsOMSourceContent, targetPackage); // Signal for the XPS OM Generation PrintWorkflowSubmittedStatus submittedStatus = PrintWorkflowSubmittedStatus.Failed; try { // Get the watermark text and pass that to the xpsReceiver string watermarkText = localStorage.GetWatermarkTextFromLocalStorage(); if (!suppressUI) { // Delete the local storage setting if this is in a Workflow session context and not a standalone run of the app localStorage.DeleteWatermarkTextFromLocalStorage(); } xpsReceiver.SetWatermarkText(watermarkText); // Get the image file information, if set, and pass that to the xpsReceiver localStorage.GetImagePropertiesFromLocalStorage(out string imageFile, out double dpiX, out double dpiY, out int imageWidth, out int imageHeight); if (imageFile != null) { xpsReceiver.SetImageProperties(imageFile, dpiX, dpiY, imageWidth, imageHeight); if (!suppressUI) { localStorage.DeleteImagePropertiesFromLocalStorage(); } } // Start the OM generation xpsReceiver.StartXpsOMGeneration(); // At this point, the xpsReceiver will receive callbacks when the XPS OM objects are available // Wait till the Whole XPS OM is generated and written to the package writer if (xpsReceiver.WaitForOMGeneration()) { UtilitiesLibrary.SendToastNotification("Background Print Workflow printing successfully completed for " + printTaskSubmittedArgs.Operation.Configuration.JobTitle, null); submittedStatus = PrintWorkflowSubmittedStatus.Succeeded; } else { UtilitiesLibrary.SendToastNotification("Background Print Workflow printing failed for " + printTaskSubmittedArgs.Operation.Configuration.JobTitle, null); } } catch (Exception ex) { string errorMessage = ex.Message; UtilitiesLibrary.SendToastNotification("Background Print Workflow printing encountered an exception: " + errorMessage, null); } finally { // All done, let the controller whether it should close the job stream or abort it printTaskSubmittedArgs.Operation.Complete(submittedStatus); // Complete the deferral taken out at the start of this function submittedDeferral.Complete(); // Complete the deferral taken out at the start of Run() runDeferral.Complete(); } }