简体   繁体   English

显示窗口后如何组织此代码以加载报告?

[英]how to organize this code to load report after window is shown?

Im trying to load a xml file into a dataset and generate crystal report with loaded data. 我试图将xml文件加载到数据集中,并使用加载的数据生成Crystal报告。

im using Crystal Reports WPF Application for the project. 我在项目中使用Crystal Reports WPF应用程序。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        reportViewer.Owner = this;

        DataSet dset = new DataSet1();

        DataSet reportData = new DataSet();
        reportData.ReadXml("http://192.168.1.10/test/persons.xml");

        dset.Tables[0].Merge(reportData.Tables[0]); 


        ReportDocument report = null;
        report = new CrystalReport1();
        report.SetDataSource(dset.Tables[0]);
        reportViewer.ViewerCore.ReportSource = report;
    }

}

This working fine and my question is how to move download xml and set report codes to a different event to load report after the application is shown? 这项工作正常,我的问题是如何在显示应用程序之后如何移动下载xml并将报告代码设置为另一个事件以加载报告?

Regards 问候

Here is the Window Lifetime Management events: ( MSDN ) 这是窗口生命周期管理事件:( MSDN

  • Activated 活性
  • Closed 关闭
  • Closing 闭幕
  • Deactivated 已停用
  • SourceInitialized 来源已初始化

I believe Activated event is what you need. 我相信您需要Activated事件。 Considering that Activated event fires not only when a window opened first time but also when ALT + TAB switches and so on so to filter out first windo opening from other Activated events just use boolean flag setting it to true when first time Activated event was handled: 考虑到Activated事件不仅会在第一次打开窗口时触发,而且还会在ALT + TAB切换等事件时触发,因此要从其他激活事件中滤除第一次窗口打开,只需在第一次处理Activated事件时使用布尔标志将其设置为true即可:

    bool firstActivateAlreadyHandled = false;
    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);

        if (!this.firstActivateAlreadyHandled)
        {
             // your code here
             this.firstActivateAlreadyHandled = true;
        }
    }

Not 100% certain what you are after, if you want to handle the download of the file elsewhere then you could read the remote file, save it locally and then once saved update the report then, if you are looking at just handling the remote file once the window shows then i'm not sure, i'm assuming you mean a form which means on a form you would do something like this: 如果您要在其他地方处理文件下载,则不能100%地确定要执行的操作,那么您可以读取远程文件,将其保存在本地,然后保存后更新报告,如果您只是在处理远程文件一旦窗口显示出来,那么我不确定,我假设您的意思是一种形式,这意味着在形式上您将执行以下操作:

Private void Form1_Shown(object sender, EventArgs e)
{
            DataSet dset = new DataSet1();

            DataSet reportData = new DataSet();
            reportData.ReadXml("http://192.168.1.10/test/persons.xml");

            dset.Tables[0].Merge(reportData.Tables[0]); 


            ReportDocument report = null;
            report = new CrystalReport1();
            report.SetDataSource(dset.Tables[0]);
            reportViewer.ViewerCore.ReportSource = report;

}

As you have said it is for WPF try.... 正如您所说的,这是WPF的尝试。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
            DataSet dset = new DataSet1();

            DataSet reportData = new DataSet();
            reportData.ReadXml("http://192.168.1.10/test/persons.xml");

            dset.Tables[0].Merge(reportData.Tables[0]); 


            ReportDocument report = null;
            report = new CrystalReport1();
            report.SetDataSource(dset.Tables[0]);
            reportViewer.ViewerCore.ReportSource = report;
}

or 要么

private void Window_ContentRendered(object sender, RoutedEventArgs e)
{
            DataSet dset = new DataSet1();

            DataSet reportData = new DataSet();
            reportData.ReadXml("http://192.168.1.10/test/persons.xml");

            dset.Tables[0].Merge(reportData.Tables[0]); 


            ReportDocument report = null;
            report = new CrystalReport1();
            report.SetDataSource(dset.Tables[0]);
            reportViewer.ViewerCore.ReportSource = report;
}

If you are concerned about UI response ie your loading stops the application flow, you can start it in a different thread. 如果您担心UI响应,即您的加载停止了应用程序流,则可以在其他线程中启动它。 Just wrap it like this. 像这样把它包起来。

public Window1()
{
    InitializeComponent();

    new Task(() =>
    {
        Dispatcher.BeginInvoke(new Action(() =>
        {
            reportViewer.Owner = this;

            DataSet dset = new DataSet1();

            DataSet reportData = new DataSet();
            reportData.ReadXml("http://192.168.1.10/test/persons.xml");

            dset.Tables[0].Merge(reportData.Tables[0]);


            ReportDocument report = null;
            report = new CrystalReport1();
            report.SetDataSource(dset.Tables[0]);
            reportViewer.ViewerCore.ReportSource = report;
        }), null);
    }).Start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM