How to show updating animation during HTTP request
I've noticed many of the apps do an "Updating..." animation at the top of the screen while they're making HTTP requests. How do I make that happen in my app while I'm waiting for an async request to return?
I suppose the method that initiates the request somehow triggers the animation, and the one that handles the eventual response stops it. Or does it automatically detect when ther开发者_如何学Pythone is network traffic?
It's a hidden ProgressBar
, set to indeterminate mode to get the dots.
Show it when you start the request and then hide it when the request completes. (or if it errors)
See comments below for performance considerations & alternatives.
e.g.
webclient.DownloadStringAsync(new Uri("http://example.com"));
progress.IsIndeterminate = true;
progress.Visibility = Visibility.Visible;
and
void dl_DownloadStringCompleted2(object sender, DownloadStringCompletedEventArgs e)
{
progress.Visibility = Visibility.Collapsed;
progress.IsIndeterminate = false;
...
}
updated with performance suggestions from comments. thanks
Using ProgressBar.IsIndeterminate eats more than 60% of CPU resources. It may kill some apps (if they need to do a lot CPU cycles) or just drain the battery.
Alternatives are described here
I would also suggest to use Progressbar from Silverligt Toolkit, due to performance reasons (it runs on diferent thread than UI, more deatils can be read here)
精彩评论