b85*_*411 10 c# printing uwp windows-10-universal
我正在尝试从我的UWP应用程序中打印出一些内容.基本上我用WebViewBrush将一些数据绘制到某些FrameworkElement(Windows.UI.Xaml.Shapes.Rectangle) - 我想在每个页面上打印其中一个矩形(每页一个矩形)
我真的希望有人可以提供一个非常简单的例子,说明如何在UWP中进行打印.我自己尝试了,我很乐意提供我的代码,但实际上有数千行 - 我从Microsoft GitHub示例中获取并尝试调整:
老实说,我认为这些例子太复杂了.我想要的只是一种非常简单的打印方式.我也找不到关于这个主题的任何教程,但我想如果有人有一个我可以开始工作的小代码片段,也许我可以在它上面构建它所以它可以使用矩形(而不是我现在正在做的事情 - 采取微软的一个很好的例子,试图找出我不需要的部分.
谢谢.我想任何能够以简单的方式回答这个问题的人都会发现这将成为未来的权威参考点 - 因为有关这个主题的在线信息是如此稀缺.
Jay*_*Zuo 19
有关如何在UWP应用程序中打印,您可以按照应用程序中的" 打印"中的步骤操作.另请参阅GitHub上的Printing示例.以下是一个简单的示例演示如何在页面上打印矩形.
在XAML中,添加打印按钮和要打印的矩形.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Button HorizontalAlignment="Center" Click="PrintButtonClick">Print</Button>
<Rectangle x:Name="RectangleToPrint"
Grid.Row="1"
Width="500"
Height="500">
<Rectangle.Fill>
<ImageBrush ImageSource="Assets/img.jpg" />
</Rectangle.Fill>
</Rectangle>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中,处理打印逻辑.
public sealed partial class MainPage : Page
{
private PrintManager printMan;
private PrintDocument printDoc;
private IPrintDocumentSource printDocSource;
public MainPage()
{
this.InitializeComponent();
}
#region Register for printing
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Register for PrintTaskRequested event
printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequested;
// Build a PrintDocument and register for callbacks
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
printDoc.Paginate += Paginate;
printDoc.GetPreviewPage += GetPreviewPage;
printDoc.AddPages += AddPages;
}
#endregion
#region Showing the print dialog
private async void PrintButtonClick(object sender, RoutedEventArgs e)
{
if (PrintManager.IsSupported())
{
try
{
// Show print UI
await PrintManager.ShowPrintUIAsync();
}
catch
{
// Printing cannot proceed at this time
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, printing can' t proceed at this time.",
PrimaryButtonText = "OK"
};
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"
};
await noPrintingDialog.ShowAsync();
}
}
private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompleted;
}
private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
#endregion
#region Print preview
private void Paginate(object sender, PaginateEventArgs e)
{
// As I only want to print one Rectangle, so I set the count to 1
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
printDoc.SetPreviewPage(e.PageNumber, this.RectangleToPrint);
}
#endregion
#region Add pages to send to the printer
private void AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(this.RectangleToPrint);
// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}
#endregion
#region Print task completed
private async void PrintTaskCompleted(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"
};
await noPrintingDialog.ShowAsync();
});
}
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7198 次 |
| 最近记录: |