如何使用抽象方法来处理委托调用?

如何使用抽象方法来处理委托调用?

问题描述:

这是基础类:如何使用抽象方法来处理委托调用?

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)); 
    } 
} 

下面是具体的实现:

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)); 

    } 
} 

有两个问题:

  1. DataResult由Visual Studio 2008中无法识别,即使它在ReportWinForm基类中。
  2. 在VS设计者2008要求来自ReportWinForm派生的类不能被编辑,即使该基类是从Form下降。

更多情况下,请参阅How can a delegate respond to multiple events with a generic and extensible class?

+1

我没有看到你定义“在ReportWinForm基类”一'DataResult'类。你必须提供关于设计者告诉你什么的更多细节,还有各种各样的理由不能编辑一个基于Form的类。 – 2013-02-14 18:27:25

+0

此外,还有“DataResult”的许多用途,请其使用VS是具有一个问题,确切的错误消息(多个)的细节。 – 2013-02-14 18:28:20

+0

此外,在什么都在你的标题描述你是否对“代表”的问题呢? – 2013-02-14 18:29:12

DataResult由Visual Studio 2008中无法识别,即使是在ReportWinForm基类。

如果真的是类,类外,应指定ReportWinForm.DataResult

VS 2008中的设计者声称从ReportWinForm派生的类无法编辑,即使基类是从Form下降的。

你确定你拥有所有DLL依赖正确的?您需要所有定义了所有基类的DLL。

顺便说一句,你可以免费download Visual Studio 2012 Express edition,如果你可以并想升级。