2013-08-22 207 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