简体   繁体   English

具有抽象方法的基类如何用于处理委托调用?

[英]How can a base class with abstract methods be used to handle delegate calls?

Here is the base class: 这是基类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

abstract class ReportWinForm : System.Windows.Forms.Form
{
    // This will be the one line of code needed in each WinForm--providing the base class a reference
    //  to the report, so it has access to the SubreportProcessing event
    protected ReportViewer WinFormReportViewer { get; set; }

    // Making this abstract requires each derived WinForm to implement GetReportData--foolproof!

    protected abstract DataResult GetReportData(SubreportProcessingEventArgs e);

    // Wire up the subreport_processing handler when any WinForm loads
    // You could override this in derived WinForms classes if you need different behavior for some WinForms,
    //  but I would bet this default behavior will serve well in most or all cases
    protected virtual void Form1_Load(object sender, EventArgs e)
    {
        WinFormReportViewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

    }

    // When the Subreport processing event fires, handle it here
    // You could also override this method in a derived class if need be
    protected virtual void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
    {
        // Get the data needed for the subreport
        DataResult dataResult = this.GetReportData(e);

        e.DataSources.Clear();
        e.DataSources.Add(new ReportDataSource(dataResult.Label, dataResult.Table));
    }
}

Here is the concrete implementation: 这是具体的实现:

public frmTestAllView()
{
    //base.WinFormReportViewer = reportViewer1; //Hook-up callbacks to the base class      ReportWinForm
    InitializeComponent();
}

private void frmTestAllView_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'AFC_ObsolescenceDataSet.up_Fill_frmInternalCaseStatus_All' table. You can move, or remove it, as needed.
    this.up_Fill_frmInternalCaseStatus_AllTableAdapter.Fill(this.AFC_ObsolescenceDataSet.up_Fill_frmInternalCaseStatus_All);

    this.reportViewer1.RefreshReport();
}

// The search parameters will be different for every winform, and will presumably
//  come from some winform UI elements on that form, e.g., parentPartTextBox.Text
protected override DataResult GetReportData(SubreportProcessingEventArgs e)
{
    // Return the data result, which contains a data table and a label which will be
    //  passed to the report data source
    // You could use DataSet in DataResult instead of DataTable if needed
    switch (e.ReportPath)
    {
        case "rptSubAlternateParts":
            return new DataResult(
                new BLL.AlternatePartBLL().GetAlternativePart(parentPartTextBox.Text)
                , "BLL_AlternatePartBLL"
            );

        case "rptSubGetAssemblies":
            return new DataResult(
                new BLL.SubAssemblyBLL().GetSubAssemblies(someOtherTextBox.Text)
                , "BLL_SubAssemblyBLL"
            );

        default:
            throw new NotImplementedException(string.Format("Subreport {0} is not implemented", e.ReportPath));

    }
}

There are two problems: 有两个问题:

  1. DataResult is unrecognized by Visual Studio 2008, even though it is in the ReportWinForm base class. DataResult由Visual Studio 2008中无法识别,即使是在ReportWinForm基类。
  2. The designer in VS 2008 claims that a class derived from ReportWinForm cannot be edited, even though the base class is descends from Form . VS 2008中的设计器声称,即使基类是Form后代,也不能编辑从ReportWinForm派生的类。

For more context, please see How can a delegate respond to multiple events with a generic and extensible class? 有关更多上下文,请参见委托如何使用通用且可扩展的类响应多个事件?

DataResult is unrecognized by Visual Studio 2008, even though it is in the ReportWinForm base class. 即使在ReportWinForm基类中,Visual Studio 2008也无法识别DataResult。

If it is really in the class, outside of the class you should specify ReportWinForm.DataResult . 如果确实类中,则应在类外部指定ReportWinForm.DataResult

The designer in VS 2008 claims that a class derived from ReportWinForm cannot be edited, even though the base class is descends from Form. VS 2008中的设计器声称,即使基类是Form的后代,也不能编辑从ReportWinForm派生的类。

Are you sure you have all DLL dependencies correct? 您确定所有DLL依赖项都正确吗? You need all DLLs in which all base classes are defined. 您需要在其中定义了所有基类的所有DLL。

By the way, you can download Visual Studio 2012 Express edition for free, if you can and want to upgrade. 顺便说一句,如果可以并且想要升级,则可以免费下载Visual Studio 2012 Express版

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

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