Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何订阅实例对象';VB6中的s事件?_C#_.net_Events_Vb6_Com Interop - Fatal编程技术网

C# 如何订阅实例对象';VB6中的s事件?

C# 如何订阅实例对象';VB6中的s事件?,c#,.net,events,vb6,com-interop,C#,.net,Events,Vb6,Com Interop,我已经用C语言编写了一个类库,我正试图在VBA(VB6)中使用它 创建我的类型库、使用regasm在计算机上注册类型库、在VB6中创建.Net对象的实例、访问属性和调用方法都很好 但我试图订阅类型库中的实例级对象引发的一些事件 下面是我在C#类中声明的一个事件处理程序(它们都是通用事件处理程序)的示例: 在C#中,我可以实例化我的phone对象并订阅事件,如下所示: MySolution.Phone _phone = new MySolution.Phone(); _phone.NewCall

我已经用C语言编写了一个类库,我正试图在VBA(VB6)中使用它

创建我的类型库、使用regasm在计算机上注册类型库、在VB6中创建.Net对象的实例、访问属性和调用方法都很好

但我试图订阅类型库中的实例级对象引发的一些事件

下面是我在C#类中声明的一个事件处理程序(它们都是通用事件处理程序)的示例:

在C#中,我可以实例化我的phone对象并订阅事件,如下所示:

MySolution.Phone _phone = new MySolution.Phone();
_phone.NewCall += AnEventHandler;
Dim thisPhone As New MySolution.Phone
事件处理程序如下所示:

using System;

namespace MySolution.Events
{
    /// <summary>
    /// A call has been created ready for use
    /// </summary>
    public class NewCall : EventArgs
    {
        /// <summary>
        /// An automatic property
        /// </summary>
        public string AutoProperty { get; private set; }

        public NewCall(string rawData)
        {
            //Some logic here
            autoProperty = rawData;
        }
    }
}
void AnEventHandler(object sender, Splicecom.Events.NewCall e)
{
    //Do stuff with e
}
Dim thisPhone As MySolution.IPhone
Set thisPhone = new MySolution.Phone
在VB6中,我可以实例化Phone对象,如下所示:

MySolution.Phone _phone = new MySolution.Phone();
_phone.NewCall += AnEventHandler;
Dim thisPhone As New MySolution.Phone
键入thisPhone。不会给我任何intellisense,但我已经习惯了,我的类中的其他方法没有intellisense,但我仍然可以调用它们,而不会出现编译/运行时错误

如下图所示,我可以看到我想要处理的事件:

但是这是库的intellisense,它是MySolution,而Phone类的实例是thisPhone

在VB6中,如何订阅MySolution.Phone对象的thisPhone实例上的NewCall事件

我试过这个:

Option Compare Database

Dim WithEvents thisPhone As MySolution.Phone

Private Sub Command0_Click()

    thisPhone = New MySolution.Phone
    thisPhone.NewCall = MyFunction()

End Sub

Private Sub MyFunction()

    'Do stuff

End Sub
单击Command0时,出现以下错误:


提示:如果可能,避免互操作。通常,完全用C#编写VB6代码更容易

您的C#必须具有comvisible属性(设置为true)和GUID。 建立一个界面
IPhone
,VB6需要这个

VB6代码应如下所示:

using System;

namespace MySolution.Events
{
    /// <summary>
    /// A call has been created ready for use
    /// </summary>
    public class NewCall : EventArgs
    {
        /// <summary>
        /// An automatic property
        /// </summary>
        public string AutoProperty { get; private set; }

        public NewCall(string rawData)
        {
            //Some logic here
            autoProperty = rawData;
        }
    }
}
void AnEventHandler(object sender, Splicecom.Events.NewCall e)
{
    //Do stuff with e
}
Dim thisPhone As MySolution.IPhone
Set thisPhone = new MySolution.Phone

然后,如果接口公开事件处理程序,则可以访问它。

VB6事件处理程序在编译时完全基于例程的名称绑定到事件。试试这个:

Option Compare Database

Dim WithEvents thisPhone As MySolution.Phone

Private Sub Command0_Click()

    thisPhone = New MySolution.Phone

End Sub

' Name of this routine is <withEventsVariableName>_<eventName> '
Private Sub thisPhone_NewCall()

    ' Do stuff '

End Sub
选项比较数据库
使用此手机作为我的解决方案。手机
专用子命令0_单击()
thisPhone=新的MySolution.Phone
端接头
'此例程的名称为u2;'
私人Sub-thisPhone_NewCall()
“做事”
端接头

好的,我会用一个叫IPhone的界面试试,希望我不会被起诉@JMK您仍然需要在VB6中正确地“连接”事件-参见我的答案