【发布时间】:2016-01-29 11:45:39
【问题描述】:
我有一个工具,它可以从数据库中读取数据,对其进行修改,然后将其显示在一个水晶报表中,该报表显示在一个单独的窗口中。
该工具有时会被没有安装 Crystal Reports 的人使用,所以我想显示特定的错误消息,它告诉用户确切地做什么(而不是现在的一般消息)。
我用这样的报告调用窗口:
try
{
ReportWindow report = new ReportWindow(resultList);
report.Show();
}
catch (Exception ex)
{
this.LogAction("Could not open the report. To view it, you need to have Crystal Reports installed.", Notification.Error);
}
(为了这个问题,catch 分支被简化了)
ReportWindow.xaml.cs
public ReportWindow(List<MeasureTestResult> measurement)
{
this._measurementList = measurement;
InitializeComponent();
}
ReportWindow.xaml
<Window x:Class="ResultFileViewer.Sources.ReportWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewer="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer">
<viewer:CrystalReportsViewer Name="CrystalReportsViewer1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Window>
ReportWindow 构造函数中的行InitializeComponent(); 会引发几个异常,例如XamlParseException 或InvalidCastException。问题是,只有XamlParseException 被捕获。其他异常仅由我的 App.xaml 的 DispatcherUnhandledException 属性处理。
这是有问题的,因为它要么意味着当 e.Handled 设置为 false 时整个应用程序被关闭,要么当 e.Handled 设置为 true 时用户关闭应用程序后仍然存在僵尸进程。
如何捕获类中当前未处理的异常,即调用报告窗口?
我试过this solution,但也没有赶上InvalidCastException。
【问题讨论】:
标签: c# wpf crystal-reports