2012-03-28 81 views
2

我只是silverlight和WCF的入門者。我遇到了Miguel A. Castro撰寫的一篇很好的文章here,該文章教導瞭如何手動添加WCF。我有合同服務設置,我唯一留下的就是從銀牌服務中獲取數據。我很難將代碼翻譯成vb.net。WCF無需在vb中添加服務引用

   BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); 
       EndpointAddress endpointAddress = new EndpointAddress("/Person.svc"); 
       IPersonService personService = new ChannelFactory<IPersonService>(basicHttpBinding, endpointAddress).CreateChannel(); 
       //+ 
       AsyncCallback asyncCallBack = delegate(IAsyncResult result) 
       { 
        Person person = ((IPersonService)result.AsyncState).EndGetPersonData(result); 
        this.Dispatcher.BeginInvoke(delegate 
        { 
         spMain.Children.Add(new TextBlock 
         { 
          Text = person.FirstName 
         }); 

        }); 
       }; 
       personService.BeginGetPersonData("F488D20B-FC27-4631-9FB9-83AF616AB5A6", asyncCallBack, personService); 

AsyncCallback asyncCallBack = delegate(IAsyncResult result) {

this.Dispatcher.BeginInvoke(delegate {

應該如何在vb.net寫?

感謝您的幫助。

+0

使用渠道工廠 – 2013-06-11 10:16:12

回答

0

請找轉化爲Vb.NET

Dim basicHttpBinding As New BasicHttpBinding() 
Dim endpointAddress As New EndpointAddress("/Person.svc") 
Dim personService As IPersonService = New ChannelFactory(Of IPersonService)(basicHttpBinding, endpointAddress).CreateChannel() 
'+ 
Dim asyncCallBack As AsyncCallback = Function(result As IAsyncResult) Do 
    Dim person As Person = DirectCast(result.AsyncState, IPersonService).EndGetPersonData(result) 
    Me.Dispatcher.BeginInvoke(Function() Do 

     spMain.Children.Add(New TextBlock() With { _ 
      Key .Text = person.FirstName _ 
     }) 
    End Function) 
End Function 
personService.BeginGetPersonData("F488D20B-FC27-4631-9FB9-83AF616AB5A6", asyncCallBack, personService) 
+0

您發佈的代碼不起作用。您使用的C#/ VB轉換器做了一些有趣的事情,因爲它不是人類,它無法理解所有事情......爲什麼當代碼在vb中時,我們必須刪除「委託」? – user1298608 2012-03-28 20:14:32

0

的C#代碼,你可以向上突破的代碼放到獨立的潛艇,做這樣的事情:

Dim asyncCallBack As AsyncCallback = AddressOf MyCallBack 

Public Sub MyCallBack(ByVal Result As IAsyncResult) 

    Me.Dispatcher.BeginInvoke(Sub() AddTextBlock()) 

End Sub 

Public Sub AddTextBlock() 

    'code to add new textblock here 

End Sub 

或看Lambda Expressions

Dim asyncCallBack As AsyncCallback = Sub(result As IAsyncResult) 

            End Sub 

and

Me.Dispatcher.BeginInvoke(Sub() 

          End Sub) 
相關問題