简体   繁体   English

通过COM到VB6实现具有事件的ac#接口

[英]Implementing a c# interface that has an event through COM to VB6

I have a VB6 class that needs to implement an interface which I created in C#. 我有一个VB6类需要实现我在C#中创建的接口。 I have been able to succesfully implement all of the properties of the interface in my vb6 class over COM, but have not been able to implement the event that it has. 我已经能够通过COM成功实现我的vb6类中的接口的所有属性,但是无法实现它具有的事件。 If I understand correctly, I will need to have the equivalent of the add and remove accessors defined to register consumers of the event, but I cannot get VB6 to accept the interface with the event defined. 如果我理解正确,我将需要相应的添加和删除访问器定义为注册事件的使用者,但我不能让VB6接受定义事件的接口。 I am setting the InterfaceType of the class to InterfaceIsIDispatch, but still get the error "Bad interface for Implements: method has underscore in name" when I try to compile. 我正在将类的InterfaceType设置为InterfaceIsIDispatch,但在我尝试编译时仍然会收到错误“实现的错误接口:方法在名称中有下划线”。 The interface does not define any methods, and neither the properties nor the event have any underscores. 接口没有定义任何方法,属性和事件都没有任何下划线。

Here is the interface definition 这是接口定义

namespace AV8B.Overlay
{
   [ComVisible(true)]
   [Guid("89519DCE-86D6-4962-8CEA-450F2AB31B4E")]
   public delegate void OverlaySymbolPropertyChangedEventHandler(object sender, OverlaySymbolPropertyChangedEventArgs e);

   [ComVisible(true)]
   [Guid("9A59EF10-B688-4af9-8C21-FB95C7ED699A")]
   public class OverlaySymbolPropertyChangedEventArgs : PropertyChangedEventArgs
   {
      OverlaySymbolPropertyChangedEventArgs(string propertyName) : base(propertyName) { }
   }

   [ComVisible(true)]
   [Guid("379B179C-85FA-4efb-8198-D1A4C80D645A")]
   public interface IOverlaySymbol : INotifyPropertyChanged
   {
      bool SelectedForProcessing { get; set; }
      int SymbolNumber { get; }
      string SymbolType { get; }
      double Latitude { get; }
      double Longitude { get; }
      string Color { get; }
      int Size { get; }

      /// <summary>
      /// This method fires the PropertyChanged event.
      /// </summary>
      void NotifyPropertyChanged();

      /// <summary>
      /// The event that fires when a property changes.
      /// </summary>
      new event OverlaySymbolPropertyChangedEventHandler PropertyChanged;
   }
}

The goal behind this is to place existing VB objects that implement this interface into a binding list which will be attached to a list grid view. 这背后的目标是将实现此接口的现有VB对象放入绑定列表中,该绑定列表将附加到列表网格视图。 The list grid view needs to know when the properties of its items change so it can update accordingly. 列表网格视图需要知道其项目的属性何时更改,以便相应地更新。

INotifyPropertyChanged is mostly used with property bindings just as you described. 正如您所描述的, INotifyPropertyChanged主要用于属性绑定。

Specifically VB6 controls (and ActiveX controls in general) have this mostly already built-in, but its a different interface. 具体来说,VB6控件(以及一般的ActiveX控件)大多已经内置,但它的界面不同。

You could perhaps work it the other way round: Your grid accepts .Net Controls with INotifyPropertyChanged and ActiveX controls with INotifyPropertySink . 您可以在其他的方式也许工作,它圆圆:你的电网接受.NET控件与INotifyPropertyChanged和ActiveX控件与INotifyPropertySink Some relevant helpers may be: 一些相关助手可能是:

Thumbs up! 竖起大拇指!

it sound very complex. 听起来很复杂。 Maybe one better approach will be 也许有一种更好的方法

1)Create one implementation of your interface it on .net and use one pattern how "Active Record" para behavior 2)After create one wrapper for that with methods simples how 1)在.net上创建一个接口的实现,并使用一个模式“Active Record”para行为2)为方法创建一个包装器后用方法简单如何

entityChange(parameters) saveChange(parameters) SaveAll CancellAll entityChange(参数)saveChange(参数)SaveAll CancellAll

from .net you can throw events to vb6 (for update grid or do something) 从.net你可以把事件扔到vb6(更新网格或做某事)

here examples list http://www.elguille.info/NET/servidorNETparaCOM/servidorNETparaCOM.htm 这里的例子列表http://www.elguille.info/NET/servidorNETparaCOM/servidorNETparaCOM.htm

mix all and do test 混合所有并做测试

There is no way to implement events in VB6, even when using native VB6 class that has events declared. 即使使用已声明事件的本机VB6类,也无法在VB6中实现事件。

What you can do to handle this impedance is to pass a callback interface to VB6 objects and implement a proxy in .Net that raises your events in callback methods implementation. 你可以做些什么来处理这种阻抗是将一个回调接口传递给VB6对象并在.Net中实现一个代理,它在回调方法实现中引发你的事件。 The proxy will obviously need to forward properties/methods too. 代理显然也需要转发属性/方法。

An option is to use standard COM events which handle the interface, subscribe, unsubscrie, etc for you. 一个选项是使用标准的COM事件来处理接口,订阅,unsubscrie等。

If you just have a single object, then normal event/delegate pairs will be exposed as COM events, but for more complex interfaces and events, you need to create your event sink interface and asociate it with the each class itself using: 如果您只有一个对象,那么普通的事件/委托对将作为COM事件公开,但是对于更复杂的接口和事件,您需要创建事件接收器接口并使用以下方法将其与每个类本身关联:

[System.Runtime.InteropServices.ComSourceInterfaces(typeof(ISystem_COMEventSink))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM