0

我正在嘗試創建一個基於貨幣轉換器的Windows Phone 7.1應用程序。我正在使用DownloadStringAsync方法獲取包含特定網站匯率的短字符串。我在Visual Studio 2010中測試過,DownloadString工作得很好。但不適用於電話應用程序。我需要在這裏做什麼?我無法真正理解它。DownloadStringAsync獲取文本字符串?

Partial Public Class MainPage 
Inherits PhoneApplicationPage 
Dim webClient As New System.Net.WebClient 
Dim a As String 
Dim b As String 
Dim result As String = Nothing 
' Constructor 
Public Sub New() 
    InitializeComponent() 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click 
    a = "USD" 
    b = "GBP" 
    webClient = New WebClient 
    Dim result As String = webClient.DownloadStringAsync(New Uri("http://rate-exchange.appspot.com/currency?from=" + a + "&to=" + b) as String) 
    TextBox1.Text = result 
End Sub 

末級

回答

2

有幾件事情錯在這裏:

  1. DownloadStringAsync沒有返回值(在C#中的術語void法)
  2. 您需要處理DownloadStringCompleted事件WebClient變量。您可以在事件處理程序中獲取結果。

你可以改變你的代碼像這樣得到上述工作:

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click 
    a = "USD" 
    b = "GBP" 
    webClient = New WebClient 
    'Add the event handler here 
    AddHandler webClient.DownloadStringCompleted, AddressOf webClient_DownloadStringCompleted    
    Dim url As String = "http://rate-exchange.appspot.com/currency?from=" & a & "&to=" & b    
    webClient.DownloadStringAsync(New Uri(url)) 
End Sub 

Private Sub webClient_DownloadStringCompleted(ByVal sender as Object,ByVal e as DownloadStringCompletedEventArgs) 
    TextBox1.Text = e.result 
End Sub