2013-08-22 209 views
0

我真的很新的WinForms和此刻的我有以下的错误使用的事件处理程序在C#:事件处理程序

Error 1 The type 'DotFlickScreenCapture.ScreenCapture' cannot be used as type parameter 'TEventArgs' in the generic type or method 'System.EventHandler'. There is no implicit reference conversion from 'DotFlickScreenCapture.ScreenCapture' to '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; 
    } 
} 

有没有人经历过这个错误?是的,我如何克服它?

回答

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; 
+1

看来这不是在4.5 –

+1

需要什么我只是发现了。试着在4.0的4.5.2项目中编译并得到错误。 4.0需要明确地说它是一个'EventArgs'而4.5 +你不需要。我简单地假设你传递的是一个'EventArgs' – Franck