我创建了一个用文本和图像打印的工作表...唯一的问题是,如果我第一次启动打印界面,图像不会出现在打印预览中...可以更正这样在预览中,我可以看到图像?
MainPage.xaml中:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="btnPrint" Click="btnPrint_Click" Content="Print" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
</Grid>
MainPage.xaml.cs中:
public sealed partial class MainPage : Page
{
PrintManager printmgr = PrintManager.GetForCurrentView();
PrintDocument printDoc = null;
PrintTask task = null;
public MainPage()
{
this.InitializeComponent();
printmgr.PrintTaskRequested += Printmgr_PrintTaskRequested;
}
private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
task = args.Request.CreatePrintTask("Print", OnPrintTaskSourceRequrested);
PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options);
deferral.Complete();
}
private async void OnPrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
var def = args.GetDeferral();
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
args.SetSource(printDoc?.DocumentSource);
});
def.Complete();
}
private void PrintDic_AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(new PageToPrint());
printDoc.AddPagesComplete();
}
private void PrintDic_Paginate(object sender, PaginateEventArgs e)
{
PrintTaskOptions opt = task.Options;
PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(e.PrintTaskOptions);
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
Page page = new PageToPrint();
Grid printableArea = (Grid)page.FindName("PrintArea");
printDoc.SetPreviewPage(e.PageNumber, printableArea);
}
private async void btnPrint_Click(object sender, RoutedEventArgs e)
{
if (printDoc != null)
{
printDoc.GetPreviewPage -= OnGetPreviewPage;
printDoc.Paginate -= PrintDic_Paginate;
printDoc.AddPages -= PrintDic_AddPages;
}
this.printDoc = new PrintDocument();
printDoc.GetPreviewPage += OnGetPreviewPage;
printDoc.Paginate += PrintDic_Paginate;
printDoc.AddPages += PrintDic_AddPages;
bool showPrint = await PrintManager.ShowPrintUIAsync();
}
}
PageToPrint.xaml:
<ScrollViewer>
<Grid x:Name="PrintArea">
<TextBlock Text="This is the text" FontSize="80"/>
<Image Source="Image/4.JPG"/>
</Grid>
</ScrollViewer>
感谢您的帮助!
答案 0 :(得分:0)
当打印预览窗口显示时,图像似乎未初始化或完全加载。您可以尝试以下方式查看在预览窗口中显示图像。
首先,在MainPage.xaml中添加一个Canvas
控件并创建其Opacity="0"
,以便我们无法在MainPage中看到它,
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Canvas Name="MyCanvas" Opacity="0"/>
<Button x:Name="btnPrint" Click="btnPrint_Click" Content="Print" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
</Grid>
然后,添加PreparePrintContent
方法并在PrintManager.ShowPrintUIAsync
事件中的btnPrint_Click
之前调用它,代码将如下所示,
private async void btnPrint_Click(object sender, RoutedEventArgs e)
{
if (printDoc != null)
{
printDoc.GetPreviewPage -= OnGetPreviewPage;
printDoc.Paginate -= PrintDic_Paginate;
printDoc.AddPages -= PrintDic_AddPages;
}
this.printDoc = new PrintDocument();
printDoc.GetPreviewPage += OnGetPreviewPage;
printDoc.Paginate += PrintDic_Paginate;
printDoc.AddPages += PrintDic_AddPages;
PreparePrintContent(new PageToPrint());
bool showPrint = await PrintManager.ShowPrintUIAsync();
}
private void PreparePrintContent(Page pageToPrint)
{
var canvas=(Canvas)this.FindName("MyCanvas");
canvas.Children.Clear();
canvas.Children.Add(pageToPrint);
}