我正在尝试将用C#编写的代码转换为VB:
// Initialize the Message Broker Events
(Application.Current as App).MessageBroker.MessageReceived += new MessageReceivedEventHandler(MessageBroker_MessageReceived);
(Application.Current as App).MessageBroker.MessageReceived += new MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived);
这是我目前所拥有的,但是当我运行它时总是会抛出错误:
' Initialize the Message Broker Events
AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_MessageReceived
AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_SpecialMessageReceived
有什么我做错了吗?
以下是我的其余代码:
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
' Initialize the Message Broker Events
'AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_MessageReceived
'AddHandler TryCast(Application.Current, App).MessageBroker.MessageReceived, AddressOf MessageBroker_SpecialMessageReceived
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived)
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)
End Sub
Private Sub MessageBroker_MessageReceived(ByVal sender As Object, ByVal e As MessageBrokerEventArgs)
' Use this event to receive all messages
Select Case e.MessageName.ToLower()
Case "message1"
' Do something with this message
Exit Select
Case "message2"
' Do something with this message
Exit Select
Case "etc."
' Do something with this message
Exit Select
Case Else
If Not String.IsNullOrEmpty(e.MessageObject.MessageBody) Then
MessageBox.Show(e.MessageObject.MessageBody)
End If
Exit Select
End Select
End Sub
Private Sub MessageBroker_SpecialMessageReceived(ByVal sender As Object, ByVal e As MessageBrokerEventArgs)
' Use this event to receive any special message objects
If TypeOf e.MessageObject Is MySpecialMessage Then
MessageBox.Show(DirectCast(e.MessageObject, MySpecialMessage).SpecialMessage)
End If
End Sub
答案 0 :(得分:3)
试试这个:
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived)
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)
我使用的http://converter.telerik.com/通常适用于我
答案 1 :(得分:0)
试试这个并查看它是否可行
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived)
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)
答案 2 :(得分:-1)
首先,C#代码不正确。
// Initialize the Message Broker Events
(Application.Current as App).MessageBroker.MessageReceived +=
new MessageReceivedEventHandler(MessageBroker_MessageReceived);
(Application.Current as App).MessageBroker.MessageReceived +=
new MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived);
它应该是,
// Initialize the Message Broker Events
((App)Application.Current).MessageBroker.MessageReceived +=
new MessageReceivedEventHandler(MessageBroker_MessageReceived);
((App)Application.Current).MessageBroker.MessageReceived +=
new MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived);
,VB就是,
CType(Application.Current, App).MessageBroker.MessageReceived +=
New MessageReceivedEventHandler(MessageBroker_MessageReceived)
CType(Application.Current, App).MessageBroker.MessageReceived +=
New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived)
as
和TryCast
首先检查实例的类型,看它是否与强制转换类型匹配。如果没有,它将返回null
,并且您将获得NullReferenceException。相反,您应该使用(Type)instance
或CType(instance, Type)
直接转换类型。从逻辑上讲,这不会有所作为,但你仍然应该了解其中的差异。 :)