Crystal Reports - Value cannot be null. Parameter name: window

柔情痞子 提交于 2020-01-13 09:07:07

问题


I recently came across an unusual error when attempting to load a crystal report form into my WPF application via a dialog, the report would show as loading for a few seconds and then throw an error stating "Value cannot be null. Parameter name: window"

This confused me, as far as i know, crystal reports doesn't use a parameter named window.

This was my code:

A simple window with a 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>

and loading the report from code behind (I've removed the standard ConnectionInfo code for simplicity)

 cryRpt = new ReportDocument();
 cryRpt.Load("report.rpt");
 ReportView.ViewerCore.ReportSource = cryRpt;

回答1:


Turns out that Crystal Reports Does indeed use a parameter named window, when it attempts to show an internal error.

CrystalReportsViewer handles internal an error and will try to show a MessageBox:

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

Show method get u201CWindow owneru201D parameter and CrystalReportsViewer tries pass Owner property CrystalReportsViewer.Owner, however by default owner is null, as such we get this unexpected error.

A simple fix for this, is in the codebehind (even when using mvvm) we simply set the owner to the current window via the following code:

ReportView.Owner = Window.GetWindow(this);

Do this in the OnLoaded Event or similar, and you will find that now you get a messagebox with an internal error thrown by CrystalReportsViewer.

Credit for this solution belongs to this thread



来源:https://stackoverflow.com/questions/24655875/crystal-reports-value-cannot-be-null-parameter-name-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!