2013-12-15 29 views
1

我用的是PrintSample併成功修改了BasePrintPage代碼如下(只是想剪下來到管理的大小和測試簡單的情況下):在PrintSample爲什麼我的Windows應用商店應用沒有在打印預覽中顯示任何內容?

 protected PrintDocument printDocument = null; 
     protected IPrintDocumentSource printDocumentSource = null; 
     internal List<UIElement> printPreviewElements = new List<UIElement>(); 
     protected event EventHandler pagesCreated; 

     protected void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e) 
     { 
      PrintTask printTask = null; 
      printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested => 
      { 
       printTask.Completed += async (s, args) => 
       { 
        if (args.Completion == PrintTaskCompletion.Failed) 
        { 
         await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => 
         { 
          MessageDialog dialog = new MessageDialog("Something went wrong while trying to print. Please try again."); 
          await dialog.ShowAsync(); 
         }); 
        } 
       }; 
       sourceRequested.SetSource(printDocumentSource); 
      }); 
     } 

     protected void RegisterForPrinting() 
     { 
      printDocument = new PrintDocument(); 
      printDocumentSource = printDocument.DocumentSource; 
      printDocument.Paginate += CreatePrintPreviewPages; 
      printDocument.GetPreviewPage += GetPrintPreviewPage; 
      printDocument.AddPages += AddPrintPages; 
      PrintManager printMan = PrintManager.GetForCurrentView(); 
      printMan.PrintTaskRequested += PrintTaskRequested; 
     } 

     protected void UnregisterForPrinting() 
     { 
      if (printDocument != null) 
      { 
       printDocument.Paginate -= CreatePrintPreviewPages; 
       printDocument.GetPreviewPage -= GetPrintPreviewPage; 
       printDocument.AddPages -= AddPrintPages; 
       PrintManager printMan = PrintManager.GetForCurrentView(); 
       printMan.PrintTaskRequested -= PrintTaskRequested; 
      } 
     } 

     protected void CreatePrintPreviewPages(object sender, PaginateEventArgs e) 
     { 
      printPreviewElements.Clear(); 
      PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions); 
      PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); 
      AddOnePrintPreviewPage(pageDescription); 
      if (pagesCreated != null) 
      { 
       pagesCreated.Invoke(printPreviewElements, null); 
      } 
      ((PrintDocument)sender).SetPreviewPageCount(printPreviewElements.Count, PreviewPageCountType.Intermediate); 
     } 

     protected void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e) 
     { 
      ((PrintDocument)sender).SetPreviewPage(e.PageNumber, printPreviewElements[e.PageNumber - 1]); 
     } 

     protected void AddPrintPages(object sender, AddPagesEventArgs e) 
     { 
      foreach (UIElement element in printPreviewElements) 
      { 
       printDocument.AddPage(element); 
      } 
      ((PrintDocument)sender).AddPagesComplete(); 
     } 

     protected void AddOnePrintPreviewPage(PrintPageDescription printPageDescription) 
     { 
      TextBlock block = new TextBlock(); 
      block.Text = "This is an example."; 
      block.Width = printPageDescription.PageSize.Width; 
      block.Height = printPageDescription.PageSize.Height; 
      printPreviewElements.Add(block); 
     } 
     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      RegisterForPrinting(); 
     } 

     protected override void OnNavigatedFrom(NavigationEventArgs e) 
     { 
      UnregisterForPrinting(); 
     } 

這工作好得很,顯示帶有TextBox文本的打印預覽頁面,「這是一個示例」。但是,當我將這段代碼放到我自己的應用程序中時,試圖從一個按鈕調用打印會顯示一個空白頁面。那麼我在這裏錯過了什麼?什麼是我忘了做的PrintSample?

回答

1

哇,你們不會相信這個......我唯一需要做的就是將所要求的主題設置爲Light ...否則,我想它不知道給文本框的風格是什麼......太可笑了。你是認真的嗎!?我不得不花上幾個小時,儘可能多地從打印樣本中進行黑客入侵......因此我搞砸了。

不管怎麼說,把RequestedTheme="Light"在App.xaml中:

<Application 
    x:Class="BlahBlahBlah.AppBlah" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="BlahBlahBlah" RequestedTheme="Light"> 
</Application> 

編輯:或者你可以逃脫只是將TextBlock的前景屬性設置爲顏色黑色的一個新的SolidColorBrush。

相關問題