我正在尝试创建一个Windows Phone 7.1应用程序,基本上是一个货币转换器。我使用DownloadStringAsync方法从特定网站获取包含汇率的短字符串。我在Visual Studio2010中测试过,DownloadString工作得很好。但不适用于手机应用程序。我需要在这里做什么?我真的搞不懂这件事。
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结束类
发布于 2014-07-23 11:57:31
这里有一些错误的地方:
DownloadStringAsync不返回值( C#术语中的void方法),您需要处理DownloadStringCompleted DownloadStringCompleted事件。您可以在事件处理程序中获取结果。您可以将您的代码更改为如下所示,以使上面的代码正常工作:
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发布于 2020-05-07 20:41:04
只需使用DownloadStringTaskAsync即可
Using WebClient As WebClient = New WebClient
Return Await WebClient.DownloadStringTaskAsync(New Uri(myurl))
End Usinghttps://stackoverflow.com/questions/24901275
复制相似问题