/// <summary> /// Sends a ink status query to the selected printer. /// </summary> /// <param name="sender" type = "Windows.UI.Xaml.Controls.Button">A pointer to the button that the user hit to enumerate printers</param> /// <param name="e">Arguments passed in by the event.</param> async void GetInkStatus(object sender, RoutedEventArgs e) { if (AssociatedPrinters.Items.Count > 0) { // Get the printer that the user has selected to query. ComboBoxItem selectedItem = AssociatedPrinters.SelectedItem as ComboBoxItem; // The interfaceId is retrieved from the detail field. string interfaceId = selectedItem.DataContext as string; try { object context = Windows.Devices.Printers.Extensions.PrintExtensionContext.FromDeviceId(interfaceId); // Use the PrinterHelperClass to retrieve the bidi data and display it. printHelper = new PrintHelperClass(context); try { BidiOutput.Text = await printHelper.GetInkLevelAsync(); rootPage.NotifyUser("Ink level query successful", NotifyType.StatusMessage); } catch (Exception) { rootPage.NotifyUser("Ink level query unsuccessful", NotifyType.ErrorMessage); } } catch (Exception) { rootPage.NotifyUser("Error retrieving PrinterExtensionContext from InterfaceId", NotifyType.ErrorMessage); } } }
/// <summary> /// Invoked when this page is navigated away from. /// </summary> /// <param name="e">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> protected override void OnNavigatedFrom(NavigationEventArgs e) { // Unsubscribe from the OnInkLevelReceived event. if (printHelper != null) { printHelper.OnInkLevelReceived -= OnInkLevelReceived; printHelper = null; } }
/// <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 Setup Event /// Raised when the source app sets the Print Ticket. This event provides a callback method for the app to signal it is done handling this event /// and optionally specifying the need for UI, which will cause the UI part of this App to be launched in OnActivated() with ActivationKind.PrintWorkflowForegroundTask . /// </summary> /// <param name="sessionManager">Session manager</param> /// <param name="printTaskSetupArgs">Has the Configuration, which include the PrintTicket, and other information</param> private void OnSetupRequested(PrintWorkflowBackgroundSession sessionManager, PrintWorkflowBackgroundSetupRequestedEventArgs printTaskSetupArgs) { // Request a deferral if any of the calls here might result in an Async method being called 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; // Check to see if user wanted to use the watermark already when the program was run "standalone" and saved in local storage // and not have UI every time. It can always be reset by running the app again and unchecking the check box localStorage = new LocalStorageUtilities(); suppressUI = localStorage.GetUseStandaloneSettings(); if (!suppressUI) { // Indicate that we need to launch the UI, in this example, because we need to get watermarking text, and optionally an image printTaskSetupArgs.SetRequiresUI(); // Set storage prefix so that foreground and background can pass properties string localStorageVariablePrefix = string.Format("{0}::{1}::", sourceApplicationName, sessionId.Substring(0, 8)); localStorage.SetStorageKeyPrefix(localStorageVariablePrefix); } else { // Use unprefixed default values set in "standalone" mode, without showing UI when activated in workflow mode localStorage.SetStorageKeyPrefix(""); } // Get the printer name try { // Get the PrinterExtensionContextNative from the activation arguments IntPtr ptr = GetPrintWorkflowConfigurationNativeAsIntPtr(printTaskSetupArgs); // Create the Print Helper PrintHelperClass printHelper = new PrintHelperClass(InitializationType.PrinterExtensionContextNativeType, (ulong)ptr); string printerName = printHelper.GetPrinterName(); // Save the name of the printer localStorage.SavePrinterNameToLocalStorage(printerName); } catch (Exception ex) { string errorMessage = ex.Message; Debug.WriteLine(errorMessage); } finally { // Complete the deferral taken out at the start of OnSetupRequested setupRequestedDeferral.Complete(); } }
public Preferences() { this.InitializeComponent(); configuration = rootPage.Config; printerExtensionContext = rootPage.Context; printHelper = new PrintHelperClass(printerExtensionContext); // Disable scenario navigation by hiding the scenario list UI elements ((UIElement)rootPage.FindName("Scenarios")).Visibility = Windows.UI.Xaml.Visibility.Collapsed; ((UIElement)rootPage.FindName("ScenarioListLabel")).Visibility = Windows.UI.Xaml.Visibility.Collapsed; ((UIElement)rootPage.FindName("DescriptionText")).Visibility = Windows.UI.Xaml.Visibility.Collapsed; DisplaySettings(); }
/// <summary> /// Sends a ink status query to the selected printer. /// </summary> /// <param name="sender" type = "Windows.UI.Xaml.Controls.Button">A pointer to the button that the user hit to enumerate printers</param> /// <param name="e">Arguments passed in by the event.</param> void GetInkStatus(object sender, RoutedEventArgs e) { if (AssociatedPrinters.Items.Count > 0) { // Get the printer that the user has selected to query. ComboBoxItem selectedItem = AssociatedPrinters.SelectedItem as ComboBoxItem; // The interfaceId is retrieved from the detail field. string interfaceId = selectedItem.DataContext as string; try { // Unsubscribe existing ink level event handler, if any. if (printHelper != null) { printHelper.OnInkLevelReceived -= OnInkLevelReceived; printHelper = null; } object context = Windows.Devices.Printers.Extensions.PrintExtensionContext.FromDeviceId(interfaceId); // Use the PrinterHelperClass to retrieve the bidi data and display it. printHelper = new PrintHelperClass(context); try { printHelper.OnInkLevelReceived += OnInkLevelReceived; printHelper.SendInkLevelQuery(); rootPage.NotifyUser("Ink level query successful", NotifyType.StatusMessage); } catch (Exception) { rootPage.NotifyUser("Ink level query unsuccessful", NotifyType.ErrorMessage); } } catch (Exception) { rootPage.NotifyUser("Error retrieving PrinterExtensionContext from InterfaceId", NotifyType.ErrorMessage); } } }
/// <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(); } }