[英]Printing in Silverlight/Lightswitch using XAML and custom controls
我正在使用Lightswitch编写Silverlight应用程序。 我想打印某些屏幕上显示的数据。
我发现该教程可以在Silverlight / Lightswitch中进行打印。 它描述了如何使用可以打印的XAML创建自定义控件。 该控件如下所示:
在后台,您可以看到控件在Silverlight应用程序中的外观。 该控件包含一个按钮和一个网格:
<StackPanel>
<Button Content="Print" Name="btnPrint" Click="btnPrint_Click" />
<Grid x:Name="LayoutRoot">
<!-- grid code goes here -->
<!-- some more code an closing tags -->
使用Silverlight的打印API,可在自定义控件中按以下方式完成打印:
PrintDocument printInvoice = new PrintDocument();
private void btnPrint_Click(object sender, System.Windows.RoutedEventArgs e){
printInvoice.PrintPage +=
new EventHandler<PrintPageEventArgs>(printInvoice_PrintPage);
}
void printInvoice_PrintPage(object sender, PrintPageEventArgs e){
e.PageVisual = LayoutRoot;
}
由于使用了e.PageVisual = LayoutRoot
,因此我们仅在打印输出中看到表格,而不是按钮。 没关系,但是我想对打印布局使用单独的XAML。 我的目标是仅在Silverlight应用程序上显示“ Print
”按钮,并在单独的XAML中定义打印布局。
因此,我刚刚开始创建第二个XAML作为SilverlightControl并尝试使用它:
MyPrintLayout mpl = new MyPrintLayout();
void printArtikels_PrintPage(object sender, PrintPageEventArgs e){
e.PageVisual = mpl.LayoutRoot;
}
但是我收到错误消息“ Das Element ist bereits das untergeordnete Element eines anderen Elements”(英语:“该元素已经是另一个元素的子元素”)。 这个问题中也讨论了此错误,但它不能解决我的问题。
当我在Silverlight应用程序中包含MyPrintLayout
,它显示没有问题(其中只有一些文本可以测试功能)。
看来我这样做完全是错误的。 我如何实现我的目标?
mpl.LayoutRoot已经是mpl的子元素。 尝试这个:
void printArtikels_PrintPage(object sender, PrintPageEventArgs e){
MyPrintLayout mpl = new MyPrintLayout();
e.PageVisual = mpl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.