我試圖把這段代碼轉換寫在C#到VB:轉換事件處理從C#代碼到VB.NET
// 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
[此網站](http://www.developerfusion.com/tools/convert/csharp-to-vb/)是轉換.net語言的好工具。 – Jim 2011-12-16 14:40:21