大家好,我一直在使用 WPF C# 工作和搜索报告,并找到了一份不错的报告,而且也很简单,找到了这个 link并使用它。所以我尝试使用它,请检查我的代码,
在我的 EmployeeProfileWindow
中的打印按钮中,
private void btnprintviolation_Click(object sender, RoutedEventArgs e)
{
ReportViolationWindow NewReportViolationWindow = new ReportViolationWindow();
//Windows.Add(NewReportViolationWindow);
GlobalVar.ViolationEmpNum = txtdispid.Text;
GlobalVar.ViolationRefNumToPrint.Clear();
for (int i = 0; i < lvviolations.Items.Count; i++)
{
GlobalVar.ViolationRefNumToPrint.Add(((EmpViolationObject)lvviolations.Items[i]).VioRefNum);
}
NewReportViolationWindow.Show();
}
因此,如果我单击该按钮,它将出现一个新的窗口名称 NewReportViolationWindow
。我将按照开源示例中的内容在模板文件夹中进行复制或编辑。我创建了名为 ReportViolation
的报告,
现在这是 NewReportViolationWindow
中的代码。
ReportDocument reportDocument = new ReportDocument();
string ats = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
StreamReader reader = new StreamReader(new FileStream(ats.ToString() + @"\Template\ReportViolation.xaml", FileMode.Open, FileAccess.Read));
reportDocument.XamlData = reader.ReadToEnd();
reportDocument.XamlImagePath = Path.Combine(ats.ToString(), @"Template\");
reader.Close();
DateTime dateTimeStart = DateTime.Now; // start time measure here
List<ReportData> listData = new List<ReportData>();
//foreach (string item in GlobalVar.ViolationRefNumToPrint)
for (int i = 0; i < 5 ; i++)
{
ReportData data = new ReportData();
data.ReportDocumentValues.Add("PrintDate", DateTime.Now);
data.ReportDocumentValues.Add("EmpIDNum", NewIDNumber.ToString());
data.ReportDocumentValues.Add("EmpName", NewEmpName.ToString());
data.ReportDocumentValues.Add("EmpPosition", NewPosition.ToString());
//data.ReportDocumentValues.Add("VioRefCode", item.ToString());
listData.Add(data);
}
XpsDocument xps = reportDocument.CreateXpsDocument(listData);
documentViewer.Document = xps.GetFixedDocumentSequence();
// show the elapsed time in window title
Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms";
}
catch (Exception ex)
{
// show exception
MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
}
现在,当我运行我的应用程序并单击打印按钮时。有时,一开始它会打开 NewReportViolationWindow 且没有错误,但当我尝试关闭报告或再次单击该按钮时,它会给出一条消息,
Specified Visual is already a child of another Visual or the root of the component target
这是错误的图像,
我认为问题是当我调用打印报告时打印按钮后面的代码,嗯有人可以吗?请...:)
第二次编辑
关于您的问题:- 您说报告窗口通常打开时不会出现错误 第一次,但之后就没有了?
是的,正确..
- 是否有正在使用的任何共享资源
ReportViolationWindow
?
抱歉,我不知道,因为我所做的只是遵循开源中的示例。
- 您如何处置/处理关闭
ReportViolationWindow
?
到目前为止,我仍然没有正确关闭我的 ReportViolationWindow
的代码。当我点击关闭按钮时,就这样了,对此感到抱歉。 :(
- 您是否保留对此
ReportViolationWindow
的任何其他引用 实例?
没有。据我所知。
最佳答案
您必须先将视觉对象与其当前父级“分离”,然后才能将其添加到新父级 - 其原因主要是由于渲染和合成引擎的工作方式;如果原始父元素不知道它不再负责渲染该子元素,并且 WPF 允许您将其附加到另一个父元素,那么在最好的情况下,您将渲染重复的视觉效果,而在最坏的情况下在这种情况下,您可能会陷入无限循环!
由于添加/删除子元素的责任在于父元素,因此您需要在父元素级别处理此问题,通常通过调用 RemoveLogicalChild
或 RemoveVisualChild
(或者理想情况下,从原始 ItemsSource 中删除项目本身并将其添加到新项目中)
编辑:从技术上讲,第一段是正确的,但我认为第二段不适用于您......在查看 WpfReports on CodePlex 处的 ReportPaginator
类的源代码之后,我注意到以下几点:
- 我相信最新版本与您的版本不同:# 行不太匹配。也许这是一个已修复的错误?
- (挑剔)我不喜欢这段代码的结构(报告引擎)...ArrayLists?单行 if/else/return 语句? 呃
现在,解决您的实际问题:
您说报告窗口通常第一次打开时不会出现错误,但之后就不会了?
ReportViolationWindow
中是否正在使用任何共享资源?您如何处置/处理
ReportViolationWindow
的关闭?您是否保留对此
ReportViolationWindow
实例的任何其他引用?
我会尝试的一件事是,只是为了查看错误是否继续存在,就是在创建它的窗口中声明一个 NewReportViolationWindow
类型的成员变量(EmployeeProfileWindow
),而不是这个:
private void btnprintviolation_Click(object sender, RoutedEventArgs e)
{
ReportViolationWindow NewReportViolationWindow = new ReportViolationWindow();
尝试如下:
ReportViolationWindow _reportViolationWindow;
private void btnprintviolation_Click(object sender, RoutedEventArgs e)
{
if(_reportViolationWindow != null)
{
_reportViolationWindow.Close();
_reportViolationWindow = null;
}
_reportViolationWindow = new ReportViolationWindow();
关于c# - 指定的 Visual 已经是另一个 Visual 的子级或组件目标的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426227/