-4

我正在尝试管理一些事件。

我有我的主要项目,用 C# 编写,我添加了另一个项目,用 VB.NET 编写。

在 VB.NET 项目中,我有一个类,引发一个事件:

Public Class newMessageArgs
    Inherits EventArgs
    Public Property messageCode As String
    Public Property appName As String
End Class

Public Class EventRaiser

    Event NewMessage As EventHandler

    Sub MessageEvent(ByVal typeEvent As String, ByVal messageCode As String, appName As String)
        If typeEvent = "newMessage" Then
            Dim args As newMessageArgs = New newMessageArgs()
            args.appName = appName
            args.messageCode = messageCode
            RaiseEvent NewMessage(Me, args)
        End If
    End Sub
End Class

引发事件的方法是从我的 VB.NET 代码中调用的,但我必须使用我的 C# 代码来处理它。我该怎么做?

此事件未绑定到任何 xaml 元素。

代码更新:

试图处理事件:

截图:http: //imgur.com/JmdA7YM

attivazione.EventRaiser raiser = new attivazione.EventRaiser();

raiser.NewMessage += raiser_NewMessage; //error: "no overloads for raiser_NewMessage matches the delegate system.eventhandler", underlining red raiser_NewMessage



private void raiser_NewMessage(object sender, newMessageArgs e)
{
   //handle your event here
}

Attivazione 是 VB.NET 中引发事件的类的名称。

4

3 回答 3

2

修改您的 VB 代码以使用与“newMessageArgs”完全匹配的委托:

Public Delegate Sub CustomEventHandler(ByVal sender As Object, ByVal e As newMessageArgs)
Public Event NewMessage As CustomEventHandler
于 2014-09-26T15:30:40.327 回答
2

您的vb.net代表签名不正确。您需要创建一个NewMessage与您的事件参数匹配的委托。

Public Class NewMessageEventArgs
    Inherits EventArgs

    Public Property MessageCode As String
    Public Property AppName As String

End Class

Public Delegate Sub NewMessageEventHandler(sender As Object, e As NewMessageEventArgs)

Public Class EventRaiser

    Public Event NewMessage As NewMessageEventHandler

    Public Sub MessageEvent(ByVal typeEvent As String, ByVal messageCode As String, appName As String)
        If (typeEvent = "newMessage") Then
            RaiseEvent NewMessage(Me, New NewMessageEventArgs() With {.AppName = appName, .MessageCode = messageCode})
        End If
    End Sub

End Class
于 2014-09-26T15:53:51.647 回答
1

你打算这样吗?

var raiser = new EventRaiser();
raiser.NewMessage += raiser_NewMessage;

private void raiser_NewMessage(object sender, EventArgs e)
{
    //handle your event here
}

但是newMessageArgs类和NewMessage事件应该在您的 VB 代码中公开。

当然,您应该使用 VB 代码将程序集的引用添加到 C# 项目中。

于 2014-09-26T14:23:24.160 回答