1

我正在尝试创建一个 Windows Phone 7.1 应用程序,基本上是一个货币转换器。我正在使用DownloadStringAsync方法从特定网站获取包含汇率的短字符串。我在 Visual Studio 2010 中测试过,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

结束类

4

2 回答 2

3

这里有一些问题:

  1. DownloadStringAsync不返回值(voidC# 术语中的方法)
  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
于 2014-07-23T03:57:31.223 回答
1

只需使用DownloadStringTaskAsync

Using WebClient As WebClient = New WebClient
    Return Await WebClient.DownloadStringTaskAsync(New Uri(myurl))
End Using
于 2020-05-07T12:41:04.193 回答