Crystal Reports - 值不能为空.参数名称:window

Ben*_*Ben 6 c# wpf crystal-reports

我最近在尝试通过对话框将水晶报表形式加载到我的WPF应用程序时遇到了一个异常错误,报告显示为加载几秒钟然后抛出错误,指出" 值不能为空.参数名称:窗口 "

这让我很困惑,据我所知,水晶报告不使用名为window的参数.

这是我的代码:

一个简单的窗口 CrystalReportsViewer

<Window x:Class="Client.Views.ReportsWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
    Title="ReportsWindowView" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
    <my:CrystalReportsViewer ShowOpenFileButton="True" Grid.Column="1" x:Name="ReportView"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

并从后面的代码加载报告(为了简单起见,我删除了标准的ConnectionInfo代码)

 cryRpt = new ReportDocument();
 cryRpt.Load("report.rpt");
 ReportView.ViewerCore.ReportSource = cryRpt;
Run Code Online (Sandbox Code Playgroud)

Ben*_*Ben 6

事实证明Crystal Reports在尝试显示内部错误时确实使用了名为window的参数.

CrystalReportsViewer 处理内部错误并尝试显示MessageBox:

System.Windows.MessageBox.Show(Window owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon)

显示方法获取u201CWindow owneru201D参数和CrystalReportsViewer尝试传递所有者属性CrystalReportsViewer.Owner,但默认情况下所有者为null,因此我们得到此意外错误.

一个简单的解决方法是在代码隐藏中(即使使用mvvm),我们只需通过以下代码将所有者设置为当前窗口:

ReportView.Owner = Window.GetWindow(this);
Run Code Online (Sandbox Code Playgroud)

在OnLoaded事件或类似事件中执行此操作,您会发现现在您收到一个内部错误引发的消息框CrystalReportsViewer.

此解决方案的功劳属于此线程