C# 如何在打印预览界面中显示多个页面,并在UWP应用程序中打印所有页面?
我需要通过我的UWP应用程序打印列表。因为列表可以有N个项目,所以我需要在打印预览中根据这一点显示页面,然后将它们全部打印出来。 我尝试了一些场景,并查看了微软提供的UWP打印应用程序示例,但它们使用RichTextBlock在内容溢出时显示所有页面。但是我有一个列表,所以我不知道我的内容何时会溢出。它只显示打印预览UI中的第一页 我也根据A4纸的大小计算了总页数。 下面是我的C#代码:C# 如何在打印预览界面中显示多个页面,并在UWP应用程序中打印所有页面?,c#,xaml,printing,uwp,C#,Xaml,Printing,Uwp,我需要通过我的UWP应用程序打印列表。因为列表可以有N个项目,所以我需要在打印预览中根据这一点显示页面,然后将它们全部打印出来。 我尝试了一些场景,并查看了微软提供的UWP打印应用程序示例,但它们使用RichTextBlock在内容溢出时显示所有页面。但是我有一个列表,所以我不知道我的内容何时会溢出。它只显示打印预览UI中的第一页 我也根据A4纸的大小计算了总页数。 下面是我的C#代码: if(PrintManager.IsSupported()) { printDoc=新的PrintDocum
if(PrintManager.IsSupported())
{
printDoc=新的PrintDocument();
printDocSource=printDoc.DocumentSource;
_totalPages=(int)Math.天花(wholeItemsListForPDF.Count/(double)15);
tempList=new ManifestPDFDataModel();
tempList=_manifestPDFDataModel;
printDoc.Paginate+=分页标签;
对于(int i=1;i
{
ContentDialog noPrintingDialog=新建ContentDialog()
{
Title=“打印错误”,
Content=“\n打印失败。”,
PrimaryButtonText=“确定”
};
printMan.PrintTaskRequested-=PrintTaskRequestedManifestLabel;
printDoc.Paginate-=分页标签;
printDoc.GetPreviewPage-=GetPreviewPageManifestLabel;
printDoc.AddPages-=AddPagesManifestLabel;
等待noPrintingDialog.ShowAsync();
});
}
其他的
{
printMan.PrintTaskRequested-=PrintTaskRequestedManifestLabel;
//printDoc.Paginate-=Paginate;
//printDoc.GetPreviewPage-=GetPreviewPage;
//printDoc.AddPages-=AddPages;
}
}
#端区
XAML页面代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30*"/>
<RowDefinition Height="70*"/>
</Grid.RowDefinitions>
<Grid BorderBrush="Black" BorderThickness="2" Margin="30">
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Manifest PDF Report" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="Function" Foreground="Black" Grid.Row="1" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="Counts" Grid.Row="2" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock x:Name="PrintedValue" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="RouteValue" Foreground="Black" Grid.Row="1" FontWeight="Bold" Margin="20,20,0,0"/>
<TextBlock x:Name="BatchIDValue" Grid.Row="2" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
</Grid>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image x:Name="BarcodeImage" Margin="20,0,0,0" HorizontalAlignment="Center"/>
<TextBlock x:Name="BatchBarcodeText" Grid.Row="1" Foreground="Black" FontWeight="Bold" Margin="20,20,0,0"/>
</Grid>
</Grid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Delivered By :" Foreground="Black" Margin="20,0,0,0" VerticalAlignment="Center"/>
<TextBlock Text="Batch Delivery Time :" Foreground="Black" Grid.Column="1" Margin="20,0,0,0" VerticalAlignment="Center"/>
</Grid>
</Grid>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="90*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock FontWeight="Bold" Text="BILL#" Foreground="Black" TextDecorations="Underline" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="1" Foreground="Black" TextDecorations="Underline" Text="Carrier" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="2" Foreground="Black" TextDecorations="Underline" Text="Package" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="3" Foreground="Black" TextDecorations="Underline" Text="Location" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="4" Text="ItemType" Foreground="Black" TextDecorations="Underline" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="5" Foreground="Black" TextDecorations="Underline" Text="Deliver To" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="6" Foreground="Black" TextDecorations="Underline" Text="Sender" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="7" Foreground="Black" TextDecorations="Underline" Text="Date" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="8" Foreground="Black" TextDecorations="Underline" Text="PO" Margin="20,20,0,0"/>
<TextBlock FontWeight="Bold" Grid.Column="9" Foreground="Black" TextDecorations="Underline" Text="Control" Margin="20,20,0,0"/>
</Grid>
<ListView Grid.Row="1" x:Name="PDFItemsList" IsItemClickEnabled="False">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="0,0,0,1" />
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<ContentControl Style="{StaticResource EmptyContentControlStyle}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Bill}" Grid.Column="0" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding CarrierName}" HorizontalAlignment="Right" Grid.Column="1" Foreground="Black" Grid.Row="1" Margin="20,20,0,0"/>
<TextBlock Text="{Binding PackageID}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="2" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Location}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="3" Margin="20,20,0,0"/>
<TextBlock Text="{Binding ItemType}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="4" Margin="20,20,0,0"/>
<TextBlock Text="{Binding DeliverTo}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="5" Margin="20,20,0,0"/>
<TextBlock Text="{Binding Sender}" HorizontalAlignment="Right" Grid.Column="6" Foreground="Black" Margin="20,20,0,0"/>
<TextBlock Text="{Binding CreationDate}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="7" Margin="20,20,0,0"/>
<TextBlock Text="{Binding PONumber}" Foreground="Black" HorizontalAlignment="Right" Grid.Column="8" Margin="20,20,0,0"/>
<TextBlock Text="{Binding ControlNumber}" HorizontalAlignment="Right" Foreground="Black" Grid.Column="9" Margin="20,20,0,0"/>
</Grid>
</ContentControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Grid>
提前谢谢。两天后,我终于可以做了。 以下是解决方案: 我们需要使用Linq使用“Skip”和“Take”方法,直到达到列表的总页数,同时我们需要不断更新列表。总页数取决于A4大小纸张中适合的项目数量。我的情况是11。因此,我的“分页”事件如下所示:
protected FrameworkElement firstPage;
public void PaginateManifestLabel(object sender, PaginateEventArgs e)
{
// Clear the cache of preview pages
printPreviewPages.Clear();
// Clear the print canvas of preview pages
PrintCanvas.Children.Clear();
// This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
ListView lastRTBOOnPage = null;
// Get the PrintTaskOptions
PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
// Get the page description to deterimine how big the page is
PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
// We know there is at least one page to be printed. passing null as the first parameter to
// AddOnePrintPreviewPage tells the function to add the first page.
//lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);
// We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
// page has extra content
int i = 0;
while (i < _totalPages)
{
_manifestPDFDataModelforPriniPagination.ItemsPDFList = tempList.ItemsPDFList.Skip(i * 11).Take(11).ToList();
lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
i++;
}
PrintDocument printDoc = (PrintDocument)sender;
// Report the number of preview pages created
printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
}
if (PrintManager.IsSupported())
{
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
_totalPages = (int)Math.Ceiling(wholeItemsListForPDF.Count / (double)11);
tempList = new ManifestPDFDataModel();
tempList = _manifestPDFDataModel;
printDoc.Paginate += PaginateManifestLabel;
printDoc.GetPreviewPage += GetPreviewPageManifestLabel;
printDoc.AddPages += AddPagesManifestLabel;
printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequestedManifestLabel;
PreparePrintContent(new GenericManifestPDF(_manifestPDFDataModel));
try
{
// Show print UI
await PrintManager.ShowPrintUIAsync();
}
catch (Exception ex)
{
// Printing cannot proceed at this time
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, printing can' t proceed at this time.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
}
}
else
{
//Printing is not supported on this device
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing not supported",
Content = "\nSorry, printing is not supported on this device.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
}
GetPreviewPageManifestLabel:
public void GetPreviewPageManifestLabel(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
PrintDocument printDoc = (PrintDocument)sender;
printDoc.SetPreviewPage(e.PageNumber, printPreviewPages[e.PageNumber - 1]);
}
public void AddPagesManifestLabel(object sender, AddPagesEventArgs e)
{
for (int i = 0; i < printPreviewPages.Count; i++)
{
// We should have all pages ready at this point...
printDoc.AddPage(printPreviewPages[i]);
}
PrintDocument printDocument = (PrintDocument)sender;
// Indicate that all of the print pages have been provided
printDocument.AddPagesComplete();
}
public void PrintTaskRequestedManifestLabel(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequestedManifestLabel);
printTask.Options.Orientation = PrintOrientation.Landscape;
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompletedManifestLabel;
}
public void PrintTaskSourceRequestedManifestLabel(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
public async void PrintTaskCompletedManifestLabel(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
});
}
else
{
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
}
}
AddPagesManifestLabel:
public void GetPreviewPageManifestLabel(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
PrintDocument printDoc = (PrintDocument)sender;
printDoc.SetPreviewPage(e.PageNumber, printPreviewPages[e.PageNumber - 1]);
}
public void AddPagesManifestLabel(object sender, AddPagesEventArgs e)
{
for (int i = 0; i < printPreviewPages.Count; i++)
{
// We should have all pages ready at this point...
printDoc.AddPage(printPreviewPages[i]);
}
PrintDocument printDocument = (PrintDocument)sender;
// Indicate that all of the print pages have been provided
printDocument.AddPagesComplete();
}
public void PrintTaskRequestedManifestLabel(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequestedManifestLabel);
printTask.Options.Orientation = PrintOrientation.Landscape;
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompletedManifestLabel;
}
public void PrintTaskSourceRequestedManifestLabel(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
public async void PrintTaskCompletedManifestLabel(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
});
}
else
{
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
}
}
PrintTaskSourceRequestedManifestLabel:
public void GetPreviewPageManifestLabel(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
PrintDocument printDoc = (PrintDocument)sender;
printDoc.SetPreviewPage(e.PageNumber, printPreviewPages[e.PageNumber - 1]);
}
public void AddPagesManifestLabel(object sender, AddPagesEventArgs e)
{
for (int i = 0; i < printPreviewPages.Count; i++)
{
// We should have all pages ready at this point...
printDoc.AddPage(printPreviewPages[i]);
}
PrintDocument printDocument = (PrintDocument)sender;
// Indicate that all of the print pages have been provided
printDocument.AddPagesComplete();
}
public void PrintTaskRequestedManifestLabel(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequestedManifestLabel);
printTask.Options.Orientation = PrintOrientation.Landscape;
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompletedManifestLabel;
}
public void PrintTaskSourceRequestedManifestLabel(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
public async void PrintTaskCompletedManifestLabel(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
});
}
else
{
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
}
}
PrintTaskCompletedManifestLabel:
public void GetPreviewPageManifestLabel(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
PrintDocument printDoc = (PrintDocument)sender;
printDoc.SetPreviewPage(e.PageNumber, printPreviewPages[e.PageNumber - 1]);
}
public void AddPagesManifestLabel(object sender, AddPagesEventArgs e)
{
for (int i = 0; i < printPreviewPages.Count; i++)
{
// We should have all pages ready at this point...
printDoc.AddPage(printPreviewPages[i]);
}
PrintDocument printDocument = (PrintDocument)sender;
// Indicate that all of the print pages have been provided
printDocument.AddPagesComplete();
}
public void PrintTaskRequestedManifestLabel(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequestedManifestLabel);
printTask.Options.Orientation = PrintOrientation.Landscape;
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompletedManifestLabel;
}
public void PrintTaskSourceRequestedManifestLabel(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
public async void PrintTaskCompletedManifestLabel(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
});
}
else
{
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
}
}
准备打印内容:
public void PreparePrintContent(GenericManifestPDF page)
{
firstPage = null;
if (firstPage == null)
{
firstPage = page;
}
}
最后,你需要这样称呼这些事件:
protected FrameworkElement firstPage;
public void PaginateManifestLabel(object sender, PaginateEventArgs e)
{
// Clear the cache of preview pages
printPreviewPages.Clear();
// Clear the print canvas of preview pages
PrintCanvas.Children.Clear();
// This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed
ListView lastRTBOOnPage = null;
// Get the PrintTaskOptions
PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
// Get the page description to deterimine how big the page is
PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
// We know there is at least one page to be printed. passing null as the first parameter to
// AddOnePrintPreviewPage tells the function to add the first page.
//lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);
// We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
// page has extra content
int i = 0;
while (i < _totalPages)
{
_manifestPDFDataModelforPriniPagination.ItemsPDFList = tempList.ItemsPDFList.Skip(i * 11).Take(11).ToList();
lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
i++;
}
PrintDocument printDoc = (PrintDocument)sender;
// Report the number of preview pages created
printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
}
if (PrintManager.IsSupported())
{
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
_totalPages = (int)Math.Ceiling(wholeItemsListForPDF.Count / (double)11);
tempList = new ManifestPDFDataModel();
tempList = _manifestPDFDataModel;
printDoc.Paginate += PaginateManifestLabel;
printDoc.GetPreviewPage += GetPreviewPageManifestLabel;
printDoc.AddPages += AddPagesManifestLabel;
printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequestedManifestLabel;
PreparePrintContent(new GenericManifestPDF(_manifestPDFDataModel));
try
{
// Show print UI
await PrintManager.ShowPrintUIAsync();
}
catch (Exception ex)
{
// Printing cannot proceed at this time
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, printing can' t proceed at this time.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
}
}
else
{
//Printing is not supported on this device
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing not supported",
Content = "\nSorry, printing is not supported on this device.",
PrimaryButtonText = "OK"
};
printMan.PrintTaskRequested -= PrintTaskRequestedManifestLabel;
printDoc.Paginate -= PaginateManifestLabel;
printDoc.GetPreviewPage -= GetPreviewPageManifestLabel;
printDoc.AddPages -= AddPagesManifestLabel;
await noPrintingDialog.ShowAsync();
}
请检查官方打印样本。他们正在PageTopPrint框架中使用RichTextBlock。我的应用程序中没有这个。我有ListView。更好的方法是使用rendertargetbitmap将ListView制作为图像,然后打印。ListView可以包含N个记录,因此这将是一个相当长的过程和时间。您需要滚动ListView并制作多个targetbitmap,然后将targetbitmap缝合在一起