我的silverlight应用程序有一个用户控件(名为CertificatePrintControl
),用于显示和打印一些信息。
我希望,在另一个控件中,使用printCtl并在运行时打印内容。
例子是:
foreach (var certId in CertToPrint)
{
var certPrintController = new CertificatePrintControl() { PrintDocument = pd, CertGuid = certId };
certPrintController.Print();
certPrintController = null;
}
问题是在CertificatePrintControl里面有这个代码
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
var oldW = lstMainPrintCertificate.Width;
var oldH = lstMainPrintCertificate.Height;
lstMainPrintCertificate.Width = e.PrintableArea.Width;
lstMainPrintCertificate.Height = e.PrintableArea.Height;
e.PageVisual = lstMainPrintCertificate;
lstMainPrintCertificate.Width = oldW;
lstMainPrintCertificate.Height = oldH;
}
catch (Exception ex)
{
//
}
}
给出了
{System.InvalidOperationException: Element is already the child of another element.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
at System.Windows.Printing.PrintPageEventArgs.set_PageVisual(UIElement value)
at com.breathesaftey.Control.CertificatePrintControl.pd_PrintPage(Object sender, PrintPageEventArgs e)}
我可能已经理解了为什么,但我找不到解决方法。
由于