首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DownloadStringAsync来获取文本字符串?

DownloadStringAsync来获取文本字符串?
EN

Stack Overflow用户
提问于 2014-07-23 11:18:37
回答 2查看 5K关注 0票数 1

我正在尝试创建一个Windows Phone 7.1应用程序,基本上是一个货币转换器。我使用DownloadStringAsync方法从特定网站获取包含汇率的短字符串。我在Visual Studio2010中测试过,DownloadString工作得很好。但不适用于手机应用程序。我需要在这里做什么?我真的搞不懂这件事。

代码语言:javascript
复制
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

结束类

EN

回答 2

Stack Overflow用户

发布于 2014-07-23 11:57:31

这里有一些错误的地方:

  1. DownloadStringAsync不返回值( C#术语中的void方法),您需要处理DownloadStringCompleted
  2. 变量的DownloadStringCompleted事件。您可以在事件处理程序中获取结果。

您可以将您的代码更改为如下所示,以使上面的代码正常工作:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2020-05-07 20:41:04

只需使用DownloadStringTaskAsync即可

代码语言:javascript
复制
Using WebClient As WebClient = New WebClient
    Return Await WebClient.DownloadStringTaskAsync(New Uri(myurl))
End Using
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24901275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档