事件的显式接口实现必须使用事件访问器语法. [英] An explicit interface implementation of an event must use event accessor syntax.

查看:76
本文介绍了事件的显式接口实现必须使用事件访问器语法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有生成此代码...我将以下代码复制并粘贴到程序中...但是在编译时显示错误....如何解决以下错误..

i am not generating this code... i copy and paste the following code in my program... but it shows error while compile.... how to solve the following error..

public override void add_OnDeviceConnected(_IWiaEvents_OnDeviceConnectedEventHandler A_0)
        {
            lock (this)
            {
                if (m_ConnectionPoint == null)
                    Init();
                _IWiaEvents_SinkHelper _iwiaEvents_SinkHelper = new _IWiaEvents_SinkHelper();
                int i = 0;
                m_ConnectionPoint.Advise((object)_iwiaEvents_SinkHelper, ref i);
                _iwiaEvents_SinkHelper.m_dwCookie = i;
                _iwiaEvents_SinkHelper.m_OnDeviceConnectedDelegate = A_0;
                m_aEventSinkHelpers.Add((object)_iwiaEvents_SinkHelper);
            }
        }

public override void add_OnDeviceDisconnected(_IWiaEvents_OnDeviceDisconnectedEventHandler A_0)
        {
            lock (this)
            {
                if (m_ConnectionPoint == null)
                    Init();
                _IWiaEvents_SinkHelper _iwiaEvents_SinkHelper = new _IWiaEvents_SinkHelper();
                int i = 0;
                m_ConnectionPoint.Advise((object)_iwiaEvents_SinkHelper, ref i);
                _iwiaEvents_SinkHelper.m_dwCookie = i;
                _iwiaEvents_SinkHelper.m_OnDeviceDisconnectedDelegate = A_0;
                m_aEventSinkHelpers.Add((object)_iwiaEvents_SinkHelper);
            }
        }

public override void add_OnTransferComplete(_IWiaEvents_OnTransferCompleteEventHandler A_0)
        {
            lock (this)
            {
                if (m_ConnectionPoint == null)
                    Init();
                _IWiaEvents_SinkHelper _iwiaEvents_SinkHelper = new _IWiaEvents_SinkHelper();
                int i = 0;
                m_ConnectionPoint.Advise((object)_iwiaEvents_SinkHelper, ref i);
                _iwiaEvents_SinkHelper.m_dwCookie = i;
                _iwiaEvents_SinkHelper.m_OnTransferCompleteDelegate = A_0;
                m_aEventSinkHelpers.Add((object)_iwiaEvents_SinkHelper);
            }
        }


public interface _IWiaEvents_Event
    { 

        event _IWiaEvents_OnDeviceConnectedEventHandler OnDeviceConnected;
        event _IWiaEvents_OnDeviceDisconnectedEventHandler OnDeviceDisconnected;
        event _IWiaEvents_OnTransferCompleteEventHandler OnTransferComplete;

    }

public interface IWia
    {

        [DispId(1)]
        Collection Devices
        {
            get;
        }

        [MethodImpl(MethodImplOptions.InternalCall)]
        [TypeLibFunc(64)]
        [DispId(10010)]
        void _DebugDialog(int fWait);

        [MethodImpl(MethodImplOptions.InternalCall)]
        [DispId(2)]
        [return: MarshalAs(UnmanagedType.Interface)]
        Item Create([In, MarshalAs(UnmanagedType.Struct)] ref object Device);

    }

public interface Wia : IWia, _IWiaEvents_Event
    {
    }


public class WiaClass : Wia
    {  
        event _IWiaEvents_OnDeviceConnectedEventHandler _IWiaEvents_Event.OnDeviceConnected; <-- I got error here   
           
        event _IWiaEvents_OnDeviceDisconnectedEventHandler _IWiaEvents_Event.OnDeviceDisconnected; <-- I got error here   
     
        event _IWiaEvents_OnTransferCompleteEventHandler _IWiaEvents_Event.OnTransferComplete; <-- I got error here   

}



错误列表:
错误1事件的显式接口实现必须使用事件访问器语法.
错误2在类,结构或接口成员声明中无效的标记``;''.



Error list:
Error 1 An explicit interface implementation of an event must use event accessor syntax.
Error 2 Invalid token '';'' in class, struct, or interface member declaration.

推荐答案

您正在尝试显式实现具有事件的接口,如果这样做,则需要使用访问器语法;

You''re trying to explicitly implement the interface with events on it, if you do that you will need to use the accessor syntax;

public class WiaClass : Wia
{
  event _IWiaEvents_OnDeviceConnectedEventHandler _IWiaEvents_Event.OnDeviceConnected
  {
    add { throw new NotImplementedException(); }
    remove { throw new NotImplementedException(); }
  }

  // And the same for the other two events
}



您显然必须实现addremove方法.

另一种选择是不使用接口的显式实现;



You would obviously have to implement the add and remove methods.

An alternative would be to not use explicit implementation of the interface;

public class WiaClass : Wia
{
  event _IWiaEvents_OnDeviceConnectedEventHandler OnDeviceConnected;
  event _IWiaEvents_OnDeviceDisconnectedEventHandler OnDeviceDisconnected;
  event _IWiaEvents_OnTransferCompleteEventHandler OnTransferComplete;
}



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik


这篇关于事件的显式接口实现必须使用事件访问器语法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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