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.
- 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.
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.
- Open the Print Sample sample app and download the C# example to your computer.
- 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.
- Select the PrintSample solution file and click Open.
- 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.
- Click Debug > Start Without Debugging.
- Verify that, after a few seconds, the screen displays the Print JS Sample app.
- If the app runs without error, return to Visual Studio and click Debug > Stop Debugging.
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.
Add the print button-click event handler to your app's code. The app calls ShowPrintUIAsync to start printing from your app.
- Build and test your app as described previously.
To see the complete source code for this and other Windows Store app printing scenarios, see the Print Sample sample app.