我真的很陌生,在 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;
}
}
以前有没有人遇到过此错误?是这样,我该如何克服它?
ScreenCapture
类必须派生自EventArgs
类才能按您想要的方式使用。
public class ScreenCapture : EventArgs
然后(为了避免误解(它应该被命名为ScreenCaptureEventArgs
.考虑到这一点,创建一个派生自EventArgs
并包含一个属性ScreenCapture
属性ScreenCaptureEventArgs
会更容易,该属性是您已有的类的实例。
诸如此类:
public class ScreenCaptureEventArgs : EventArgs
{
public ScreenCaptureEventArgs(ScreenCapture c)
{
Capture = c;
}
public ScreenCapture Capture { get; private set; }
}
public event EventHandler<ScreenCaptureEventArgs> ScreenCaptured;