[英]Printing in Universal Apps
我正在開發Windows 10通用應用程序,該應用程序需要執行計算並根據結果生成打印輸出。 我不知道從哪里開始打印。 創建新文檔的最簡單方法是什么? 在WPF中,我們有FixedDocument和FlowDocument。 我看不到通用應用格式可用的那些。
聲明PrintManager
和PrintDocument
。 PrintManager
類型與其他支持Windows打印功能的類型一起位於Windows.Graphics.Printing
命名空間中。 PrintDocument
類型與其他支持准備XAML內容進行打印的類型一起位於Windows.UI.Xaml.Printing
命名空間中。 您可以通過在頁面上添加以下使用或Imports語句來簡化編寫打印代碼的過程。
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
PrintDocument
類用於處理應用程序和PrintManager
之間的大部分交互,但是它公開了一些自己的回調。 注冊期間,創建PrintManager
和PrintDocument
實例,並為它們的打印事件注冊處理程序。 在UWP
打印示例中,注冊是通過RegisterForPrinting
方法執行的。
public virtual void RegisterForPrinting()
{
printDocument = new PrintDocument();
printDocumentSource = printDocument.DocumentSource;
printDocument.Paginate += CreatePrintPreviewPages;
printDocument.GetPreviewPage += GetPrintPreviewPage;
printDocument.AddPages += AddPrintPages;
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequested;
}
當用戶轉到支持的頁面時,它將在OnNavigatedTo方法內啟動注冊。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Initalize common helper class and register for printing
printHelper = new PrintHelper(this);
printHelper.RegisterForPrinting();
// Initialize print content for this scenario
printHelper.PreparePrintContent(new PageToPrint());
// Tell the user how to print
MainPage.Current.NotifyUser("Print contract registered with customization, use the Print button to print.", NotifyType.StatusMessage);
}
當用戶離開頁面時,請斷開打印事件處理程序的連接。 如果您有多頁應用程序並且沒有斷開打印連接,則當用戶離開頁面然后返回頁面時,將引發異常。
您可以從您的應用程序上閱讀有關“ 打印”的更多信息,並找到提示: 提示本主題中的大多數示例均基於打印示例。 要查看完整代碼,請從GitHub上的Windows-universal-samples存儲庫下載通用Windows平台(UWP)打印示例。
對於每個頁面,一次GetPrintPreviewPage事件被終止,當我返回頁面時,該事件未被終止。 我使用此代碼來保留當前的預覽頁面。
私有異步void PrintDoc_GetPreviewPage(object p_sender,GetPreviewPageEventArgs p_args){SelectedPagePreview = p_args.PageNumber;
我正在Windows 8.1和平板電腦x64上進行測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.