Exemplo n.º 1
0
        /// <summary>
        /// Invoked when the app is launched via an activation contract. In this case we care about ActivationKind.PrintTaskSettings
        /// </summary>
        /// <param name="args">Information about why this was invoked</param>
        protected override void OnActivated(IActivatedEventArgs args)
        {
            if (args.Kind == ActivationKind.PrintTaskSettings)
            {
                Frame rootFrame = new Frame();
                if (null == Window.Current.Content)
                {
                    rootFrame.Navigate(typeof(MainPage));
                    Window.Current.Content = rootFrame;
                }
                Window.Current.Activate();

                MainPage mainPage = (MainPage)rootFrame.Content;

                // Get the PrinterExtensionContext from the activation arguments
                IntPtr ptr = GetPrinterExtensionContextAsIntPtr(args);
                // Create the Print Helper
                PrintHelperClass printHelper = new PrintHelperClass(InitializationType.PrinterExtensionContextType, (ulong)ptr);
                // Can use the Windows Runtime Component above to GetPrinterName() and do BiDi queries etc.
            }

            if (args.Kind == ActivationKind.PrintWorkflowForegroundTask)
            {
                Frame rootFrame = new Frame();
                if (null == Window.Current.Content)
                {
                    rootFrame.Navigate(typeof(WorkflowPage));
                    Window.Current.Content = rootFrame;
                }

                // Ensure the current window is active
                ActivateWindowAndSetDesiredSize();

                // Get the main page
                WorkflowPage workflowPage = (WorkflowPage)rootFrame.Content;

                // Workflow stuff here

                // Make sure the page knows it's handling a foreground task activation
                workflowPage.LaunchType = WorkflowPage.WorkflowPageLaunchType.ForegroundTask;

                // Get the activation arguments
                PrintWorkflowUIActivatedEventArgs printTaskUIEventArgs = args as PrintWorkflowUIActivatedEventArgs;

                // Get the session manager
                PrintWorkflowForegroundSession taskSessionManager = printTaskUIEventArgs.PrintWorkflowSession;

                // Add the callback handlers - these are on the main page
                taskSessionManager.SetupRequested   += workflowPage.OnSetupRequested;
                taskSessionManager.XpsDataAvailable += workflowPage.OnXpsDataAvailable;

                taskSessionManager.Start();
            }
        }
        /// <summary>
        /// Handle the Print Task XPS Data Available Event
        /// </summary>
        /// <param name="sessionManager">Session manager</param>
        /// <param name="printTaskXpsAvailableEventArgs">Has the Configuration and Controller</param>
        internal async void OnXpsDataAvailable(PrintWorkflowForegroundSession sessionManager, PrintWorkflowXpsDataAvailableEventArgs printTaskXpsAvailableEventArgs)
        {
            // Take out a deferral whilst getting the information
            Deferral xpsDataAvailableDeferral = printTaskXpsAvailableEventArgs.GetDeferral();

            if (sessionManager != null && printTaskXpsAvailableEventArgs != null)
            {
                // See if we already have the information required and it
                // were desireable to only have to enter this information once then
                // you could determine that here and not do WaitUntilTheUIHasAllRequiredData();
            }

            // Don't actually exit until all UI stuff is done
            await WaitUntilTheUIHasAllRequiredData();

            // Complete the deferral taken out at the start of OnSetupRequested
            xpsDataAvailableDeferral.Complete();
        }
        /// <summary>
        /// Handle the Print Task Setup Event
        /// </summary>
        /// <param name="sessionManager">Session manager</param>
        /// <param name="printTaskSetupArgs">Has the Configuration and Controller</param>
        internal void OnSetupRequested(PrintWorkflowForegroundSession sessionManager, PrintWorkflowForegroundSetupRequestedEventArgs printTaskSetupArgs)
        {
            // If anything asynchronous is going to be done, you need to take out a deferral here,
            // since otherwise the next callback happens once this one exits, which may be premature
            Deferral setupRequestedDeferral = printTaskSetupArgs.GetDeferral();

            // Get information about the source application, print job title, and session ID
            string sourceApplicationName = printTaskSetupArgs.Configuration.SourceAppDisplayName;
            string jobTitle  = printTaskSetupArgs.Configuration.JobTitle;
            string sessionId = printTaskSetupArgs.Configuration.SessionId;
            string localStorageVariablePrefix = string.Format("{0}::", sessionId);

            localStorage.SetStorageKeyPrefix(localStorageVariablePrefix);

            // Get the PrinterExtensionContextNative from the activation arguments
            IntPtr ptr = GetPrintWorkflowConfigurationNativeAsIntPtr(printTaskSetupArgs);

            // Create the Print Helper
            PrintHelperClass printHelper = new PrintHelperClass(InitializationType.PrinterExtensionContextNativeType, (ulong)ptr);

            printerName          = printHelper.GetPrinterName();
            WorkflowHeadingLabel = string.Format(workflowHeadingFormat, sourceApplicationName, printerName);

            // Add callback handler on main page
            try
            {
                printHelper.OnInkLevelReceived += OnQueryResultReceived;
                // Send the BiDi query
                printHelper.SendInkLevelQuery();
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                Debug.WriteLine(errorMessage);
            }
            finally
            {
                // Complete the deferral taken out at the start of OnSetupRequested
                setupRequestedDeferral.Complete();
            }
        }