Поделиться через


How to print using an in-app print button (XAML)

This tutorial shows how to add a print button to your Windows Store app using C++, C#, or Visual Basic.

By default, a user prints from an app by selecting a print device from the Devices charm. You can let the user initiate a print job from your app's UI by calling the ShowPrintUIAsync method from a button's Click event handler.

Objective: Create an app that prints its contents when a user clicks a print button in the app.

Prerequisites

  • You must be familiar with C#, C++, or Visual Basic, XAML, events, and event handling.
  • You must have Microsoft Visual Studio installed.
  • You must have a printer installed.
  • You must have a Windows Store app using C++, C#, or Visual Basic to which you want to add a print button. If you don't have your own app, you can download the Print Sample sample app and use that app.
  • Your app must already support basic Windows printing. If you don't have such an app, review Quickstart: Printing from your app to learn how to add basic Windows printing support to your app.

Instructions

1. Step 1: Open your app in Visual Studio

The procedure described in this tutorial refers to the PrintSample app from the Print Sample sample app. If you are adding a print button to your own app, open your app in Visual Studio instead of the Print Sample sample app.

  1. Open the Print Sample sample app and download the C# example to your computer.
  2. In Visual Studio, click File > Open Project and go to the folder that contains the solution file of the sample app that you downloaded in the previous step.
  3. Select the PrintSample solution file and click Open.

2. Step 2: Build and test the app

  1. Click Build > Build Solution to build the app you are working on. Make sure that there are no error messages in the Output pane at the bottom of the screen.
  2. Click Debug > Start Without Debugging.
  3. Verify that, after a few seconds, the screen displays the Print JS Sample app.
  4. If the app runs without error, return to Visual Studio and click Debug > Stop Debugging.

3. Step 3: Add the print button

  1. Add the button to your app's screen where you'd like it to appear. Make sure that it doesn't interfere with the content that you want to print.

    This example is from ScenarioInput2.xaml of the Print Sample sample app.

    <Button x:Name="invokePrintingButton" Content="Print" Margin="0,0,10,0" Click="InvokePrintButtonClick"/>
    
  2. Add the print button-click event handler to your app's code. The app calls ShowPrintUIAsync to start printing from your app.

    void ScenarioInput2::InvokePrintButtonClick(Object^ sender, RoutedEventArgs^ e)
    {
        Windows::Graphics::Printing::PrintManager::ShowPrintUIAsync();
    }
    
    private async void InvokePrintButtonClick(object sender, RoutedEventArgs e)
    {
        // Don't act when in snapped mode
        if (this.ActualWidth >= 768)
        {
            await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
        }
    }
    
    Private Async Sub InvokePrintButtonClick(sender As Object, e As RoutedEventArgs)
        ' Don't act when in snapped mode
        If Me.ActualWidth >= 768 Then
            Await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync()
        End If
    End Sub
    
  3. Build and test your app as described previously.

Summary and next steps

To see the complete source code for this and other Windows Store app printing scenarios, see the Print Sample sample app.

Printing