0

我对在 Winforms 中使用 C# 中的事件处理程序真的很陌生,目前我遇到以下错误:

错误 1 ​​类型“DotFlickScreenCapture.ScreenCapture”不能用作泛型类型或方法“System.EventHandler”中的类型参数“TEventArgs”。没有从“DotFlickScreenCapture.ScreenCapture”到“System.EventArgs”的隐式引用转换。

我已经尝试寻找一种方法来解决这个错误,但到目前为止,我的谷歌搜索还没有出现任何东西。

此错误指向的行是这一行:

  public EventHandler<ScreenCapture> capture;

据我所知,这门课:

public class ScreenCapture
{
    public delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);
    public event StatusUpdateHandler OnUpdateStatus;

    public bool saveToClipboard = true;

    public void CaptureImage(bool showCursor, Size curSize, Point curPos, Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath, string extension)
    {
        Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height);

        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);

            if (showCursor)
            {
                Rectangle cursorBounds = new Rectangle(curPos, curSize);
                Cursors.Default.Draw(g, cursorBounds);
            }
        }

        if (saveToClipboard)
        {

            Image img = (Image)bitmap;
            Clipboard.SetImage(img);

            if (OnUpdateStatus == null) return;

            ProgressEventArgs args = new ProgressEventArgs(img);
            OnUpdateStatus(this, args);
        }
        else
        {
            switch (extension)
            {
                case ".bmp":
                    bitmap.Save(FilePath, ImageFormat.Bmp);
                    break;
                case ".jpg":
                    bitmap.Save(FilePath, ImageFormat.Jpeg);
                    break;
                case ".gif":
                    bitmap.Save(FilePath, ImageFormat.Gif);
                    break;
                case ".tiff":
                    bitmap.Save(FilePath, ImageFormat.Tiff);
                    break;
                case ".png":
                    bitmap.Save(FilePath, ImageFormat.Png);
                    break;
                default:
                    bitmap.Save(FilePath, ImageFormat.Jpeg);
                    break;
            }
        }
    }
}


public class ProgressEventArgs : EventArgs
{
    public Image CapturedImage { get; private set; }
    public ProgressEventArgs(Image img)
    {
        CapturedImage = img;
    }
}

以前有没有人遇到过这个错误?是这样,我该如何克服呢?

4

1 回答 1

6

该类ScreenCapture必须派生自EventArgs要以您想要的方式使用的类。

public class ScreenCapture : EventArgs

然后(为了避免误解)它应该被命名为ScreenCaptureEventArgs. ScreenCaptureEventArgs考虑一下,创建一个派生自EventArgs并包含一个属性的类会更容易,该属性ScreenCapture是您已有的类的实例。

像那样:

public class ScreenCaptureEventArgs : EventArgs
{
    public ScreenCaptureEventArgs(ScreenCapture c)
    {
        Capture = c;
    }

    public ScreenCapture Capture { get; private set; }
}

public event EventHandler<ScreenCaptureEventArgs> ScreenCaptured;
于 2013-08-22T10:32:51.373 回答