簡體   English   中英

具有抽象方法的基類如何用於處理委托調用?

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

這是基類:

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中的設計器聲稱,即使基類是Form后代,也不能編輯從ReportWinForm派生的類。

有關更多上下文,請參見委托如何使用通用且可擴展的類響應多個事件?

即使在ReportWinForm基類中,Visual Studio 2008也無法識別DataResult。

如果確實類中,則應在類外部指定ReportWinForm.DataResult

VS 2008中的設計器聲稱,即使基類是Form的后代,也不能編輯從ReportWinForm派生的類。

您確定所有DLL依賴項都正確嗎? 您需要在其中定義了所有基類的所有DLL。

順便說一句,如果可以並且想要升級,則可以免費下載Visual Studio 2012 Express版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM