C#-从我添加的.NET文件中定义事件作为对我项目的引用

问题描述 投票:-1回答:1

我正在尝试从NET .dll file中定义一个事件,该事件是作为对我的项目的引用添加的。.dll file(PICkitS.dll)中的所有功能均正常运行,并且所有原型标头均已定义here

我只是不知道如何定义/访问事件函数。

我需要访问的“事件”功能就是这个功能(如上文.pdf中所定义):

public delegate void OnReceive(byte masterid, byte[] data, byte length, byte error,
ushort baud, double time);
///////////////////////////////////////////////////////////////////////////
//
// Triggered: When a frame is received, the DLL is in LISTEN mode and
// the frame is different than what is in the buffer.
// OR
// When a frame is received and the DLL is in DISPLAY_All mode
// AND
// In Master mode and sent a whole frame.
//
// Paramters: byte masterid - Frame ID
// byte[] data - array of data associated with ID
// - only contains data, not the ID
// byte length - bytecount of data only, not the ID
// ushort baud - value of baud rate received
// with frame data
// double time - time in seconds since last frame break
// byte error - Error code: 0 - no error
// 1 - timeout
// 2 - no sync break
// 3 - error resetting timestamp
// 4 - PKSA status error
// 5 - PKSA error event marker
//
/////////////////////////////////////////////////////////////////////////// 

问题是:

如何触发此“ OnReceive”时如何获取接收到的数据?

如何添加以此触发的功能?

此问题与定义串行端口“ DataReceived”事件有关,但是由于串行端口已内置在C#工具箱中,因此我只需双击事件属性并自动添加该函数即可添加它。

此外,也希望能获得一些参考,以更好地了解此事件处理/添加。

c# .net events dll
1个回答
0
投票

事件只不过是代表周围的添加和删除访问器,就像获取和设置属性一样。

事件的常见模式是采用2个参数:

  • 对象发送者。我很确定这是从泛型时代开始的,但即使现在,它仍然具有不限制发件人类型的优点
  • EventsArgs。或派生类。如果您有一堆参数,请将其放入自定义EventArges实现中。

例如,我可以给你INotifyPropertyChanged的代码:

    //The delegate is defined in another part of the code
    //As is the custom EventArgs class
    public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);


    //external code can only add/remove from this
    //class code can read and fully empty it
    public event PropertyChangedEventHandler PropertyChanged;  

    // This method is called by the Set accessor of each property.  
    // The CallerMemberName attribute that is applied to the optional propertyName  
    // parameter causes the property name of the caller to be substituted as an argument.  
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
    {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }  

CallerMember名称对于此特定事件非常有用。甚至有可能添加just for it。特定的“ raiseEvent”功能的想法非常普遍。有时将它们标记为保护虚拟而不是私有,因为重写它们对于继承者可能是明智的。

© www.soinside.com 2019 - 2024. All rights reserved.