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 당신은 WebClientDownloadStringCompleted 이벤트를 처리 할 필요가
  2. (C#을 측면에서 void 방법) 값을 반환하지 않습니다 변하기 쉬운. 결과 핸들러에서 결과를 얻을 수 있습니다.
  3. 당신은 위의를 얻기 위해 이런 일에 코드를 변경할 수 있습니다

    작동하는

:

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 
관련 문제