2011-12-15 85 views
1

我試圖把這段代碼轉換寫在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 
+0

[此網站](http://www.developerfusion.com/tools/convert/csharp-to-vb/)是轉換.net語言的好工具。 – Jim 2011-12-16 14:40:21

回答

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/通常很適合我

+0

同樣在這裏..這就是我如何快速得到答案.. LOL – MethodMan 2011-12-15 20:46:21

0

試試這個,看看它是否會工作

TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_MessageReceived) 
TryCast(Application.Current, App).MessageBroker.MessageReceived += New MessageReceivedEventHandler(MessageBroker_SpecialMessageReceived) 
-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) 

asTryCast首先檢查實例的類型,看它是否投類型相匹配。如果沒有,它會返回null,並且您將得到一個NullReferenceException。相反,您應該直接使用(Type)instanceCType(instance, Type)來投射類型。從邏輯上說,這不會有什麼不同,但你仍應該瞭解其中的差異。 :)