Search code examples
c#-4.0windows-runtimemicrosoft-metrocharms-bar

Metro App Detect Printer Charm Open and Close


I have a metro application and would like to detect when the Printer charm is opened and closed. Is this possible?


Solution

  • You can use PrintManager's PrintTaskRequested event. When Printer charm bar is opened, that event occurs. PrintTaskRequested event do printing using PrintTask objects. It has event called Completed event. It ouccurs when print task is completed.

    MSDN documentation of PrintTask class

    MSDN documentation of PrintManager class

    XAML

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Click="Button_Click_1" Content="Print" />
    </Grid>
    

    C#

    public sealed partial class BlankPage2 : Page
    {
        public BlankPage2()
        {
            this.InitializeComponent();
            PrintManager printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested += PrintTaskRequested;
        }
    
        private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Printer Opened");
            //TODO:
            PrintTask printTask = e.Request.CreatePrintTask(....);
            printTask.Completed += printTask_Completed;
        }
    
        void printTask_Completed(PrintTask sender, PrintTaskCompletedEventArgs args)
        {
            //TODO:
        }
    
    
        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
        }
    }