Search code examples
c#windows-phone

How to display TextBlock while a string is downloaded from server


My app for Windows Phone has a string downloaded from server. I'm trying displaying a textBlock while the download is in execution. However, my code doesn't works. The textBlock is not displayed. Why? Is there another way for this?

 WebClient webClient1 = new WebClient();
 webClient1.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
 webClient1.DownloadStringCompleted += webClient_DownloadStringCompleted1;
 webClient1.DownloadStringAsync(new Uri(string.Format(url + insCodComanda.Text + "&random=" + ran.Next().ToString(), UriKind.RelativeOrAbsolute)));

 public void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
  {

     testete.Visibility = Visibility.Visible;

  }

void webClient_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e)
    {
      testete.Visibility = Visibility.Collapsed;
    }

Solution

  • If you want to manipulate UI element like that you need to use Dispatcher

    this.Dispatcher.BeginInvoke(new Action(() => testete.Visibility = Visibility.Visible));