【问题标题】:Event handler throws error in delegate type?事件处理程序在委托类型中引发错误?
【发布时间】:2014-02-07 02:56:04
【问题描述】:

我有 Copy Proses 的活动,但在 cp.OnCopying 行中出现问题错误:

“cp_copying”匹配委托没有重载 System.EventHandler 'System.ComponentModel.ProgressChangedEventArgs'

在 cp.OnCopyingComplete 行中:

不能隐式转换类型'System.EventHandler'System.EventArgs'' 到“CustomProses.Completedelegate”

代码:

public void SalinMultipleFile1()
{
   cp.OnCopying += new EventHandler<ProgressChangedEventArgs>(cp_copying);
   cp.OnCopyingComplete += new EventHandler<EventArgs>(cp_CopySucces);
   cp.CustomCopy(DGVFile, tempFolder);
}

void cp_copying(object sender, ProgressEventArgs e)
{
   dfe.pbPerFile.Increment(e.PercentDelta);
}

void cp_CopySucces(object sender, EventArgs e)
{
    dfe.l_proses.Text = "Succes Copy";
}

类自定义散文

Class CustomProses
{
   public delegate void ProgressChangeDelegate(double Persentage, ref bool Cancel);
   public delegate void Completedelegate();
   public event ProgressChangeDelegate OnCopying;
   public event Completedelegate OnCopyingComplete;
}

有谁知道错误在哪里?

【问题讨论】:

    标签: c# delegates event-handling


    【解决方案1】:

    您需要将您的 EventHandler 分配更改为类似的内容。

    cp.OnCopying += cp_copying;
    cp.OnCopyingComplete += cp_CopySucces;
    

    您的事件处理程序还需要完全匹配事件,因此将您的事件处理程序更改为:

    void cp_copying(double value, ref bool e)
    {
        //dfe.pbPerFile.Increment(e.PercentDelta);
    }
    
    void cp_CopySucces()
    {
       // dfe.l_proses.Text = "Succes Copy";
    }
    

    【讨论】:

      【解决方案2】:

      您的 Completedelegate 不带参数并返回 void。但您正试图将它附加到您的 cp_CopySucces 上,它需要两个参数。

      您的ProgressChangeDelegate 采用一个double 和一个bool 参数,但您试图将其附加到cp_copying 方法,该方法采用objectProgressEventArgs 作为参数。

      您的方法和委托签名应该匹配。顺便说一下,错误很明显。我认为您需要阅读Delegates Tutorial

      【讨论】:

        猜你喜欢
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多