如何使用委托定义事件 [英] how define event with delegate

查看:85
本文介绍了如何使用委托定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何用委托调用事件。

我的代码是



在file1.cs //这个主要文件我必须使用EacApi



 命名空间 Program1 
{
private StatusEventHandler OnGetCamStatus;
[ComSourceInterfaces( Program1.IEacApiEvents)]
[ClassInterface(ClassInterfaceType) .None)]
[ComVisible( true )]
[Guid( ED8211C0-50A6-4873-8964-31A381D96A23)]
public class EacApi
{
public event StatusEventHandler OnGetStatus
{
[MethodImpl(MethodImplOptions.Synchronized)] add
{
this .OnGetStatus = this .OnGetStatus + value ;
}
[MethodImpl(MethodImplOptions.Synchronized)] remove
{
this .OnGetStatus = this .OnGetStatus - value ;
}
}
}
}





在file2中。 cs



 命名空间 Program1 
{
public delegate void StatusEventHandler( int iType, int iValue, int iDataSize, object data);
}
在file3.cs中

命名空间 Program1
{
[ComVisible(< span class =code-keyword> true )]
[Guid( 5D3FB90D- E41F-4412-B3D4-D6B7939E5214)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IEacApiEvents
{
[DispId( 3 )]
void OnGetCamStatus( int iType, int iValue, int iDataSize, object vData);
}
}

解决方案

没有召集代表这样的事情。委托实例是调用,这是一个非常不同的事情:来自其调用列表的所有处理程序都被调用。这样做是这样的:

  if (OnGetStatus!=  null 
OnGetStatus.Invoke( this new StatusEventArgs( / * ... * / )); // 或StatusEvenHandler签名所需的任何内容



你可以只在这个类(声明的类)中调用这个事件,在其他任何地方,这是一个重要的万无一失的功能。



在这里,我做了向上类型 StatusEvenArgs 假设您使用的是公共事件声明模式:

  class  StatusEventArgs:System.EventArgs { / *   ...定义特定事件args参数在这个班级* / } 

// ...

public class EacApi {
public event System.EventHandler< StatusEventArgs> OnGetStatus;
// ...
}





如果您正在做其他事情,请采用这种模式。如果由于某种原因,你想以其他方式调用事件,那么你应该...审查你的代码设计;它现在将要发生。如果是这种情况,请更好地解释您的最终目标,我很可能会建议解决方案。



-SA

I want to know that how to call event with delegate.
My code is

In file1.cs //this main file I have to go through using EacApi

namespace Program1
   {
     private StatusEventHandler OnGetCamStatus;
     [ComSourceInterfaces("Program1.IEacApiEvents")]
     [ClassInterface(ClassInterfaceType.None)]
     [ComVisible(true)]
     [Guid("ED8211C0-50A6-4873-8964-31A381D96A23")]
     public class EacApi
     {
         public event StatusEventHandler OnGetStatus
       {
         [MethodImpl(MethodImplOptions.Synchronized)] add
         {
           this.OnGetStatus = this.OnGetStatus + value;
         }
         [MethodImpl(MethodImplOptions.Synchronized)] remove
         {
           this.OnGetStatus = this.OnGetStatus - value;
         }
       }
   }
   }



In file2.cs

 namespace Program1
    {
      public delegate void StatusEventHandler(int iType, int iValue, int iDataSize, object data);
    }
In file3.cs

        namespace Program1
    {
      [ComVisible(true)]
      [Guid("5D3FB90D-E41F-4412-B3D4-D6B7939E5214")]
      [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
      public interface IEacApiEvents
      {
        [DispId(3)]
        void OnGetCamStatus(int iType, int iValue, int iDataSize, object vData);
        }
        }

解决方案

There is no such thing as "call a delegate". A delegate instance is invoked, which is a very different things: all the handlers from its invocation list get called. This is done like this:

if (OnGetStatus != null)
    OnGetStatus.Invoke(this, new StatusEventArgs(/*...*/)); // or whatever is required by the signature of StatusEvenHandler


You can invoke this event only in this class (declared class), nowhere else, which is an important fool-proof feature.

Here, I made up the type StatusEvenArgs assuming that you are using the common event declaration pattern:

class StatusEventArgs : System.EventArgs { /* ... specific event args parameters are defined in this class */ }

//...

public class EacApi {
   public event System.EventHandler<StatusEventArgs> OnGetStatus;
   //...
}



If you are doing something else, resort to this pattern. If, by some reason, you want to invoke event somehow else you should… review your code design; it''s now going to happen. If this is the case, better explain your ultimate goal, they I would, most likely, be able to suggest a solution.

—SA


这篇关于如何使用委托定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆