W 8.1加载BitmapImage

W 8.1 loading BitmapImage

我正在像这样从 url 加载 BitmapImage;

Uri uri = new Uri(item.url, UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
Image.Source = imgSource;

代码工作正常,但有时加载图像需要很长时间,有没有一种方法可以检测图像是否已加载以在加载时启用进度控制?

来自 MSDN BitmapImage.DownloadProgress event 页面的注释和示例:

For cases where the async loading and decoding of a BitmapImage object are long enough to be noticeable to the user, an app can handle DownloadProgress on the source and display a ProgressRing or ProgressBar to indicate the progress state. These might display in the UI region that the image eventually displays in, or somewhere else in the UI. Use DownloadProgressEventArgs.Progress to modify the UI for a ProgressBar.

// somewhere in initialization
bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bi_DownloadProgress);
bitmapImage.ImageOpened += new EventHandler<ExceptionRoutedEventArgs>(bi_ImageOpened);
...
//progressBar is an existing control defined in XAML or extracted from a XAML template

void bi_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
    progressBar.Value = e.Progress;
}
void bi_ImageOpened(object sender, RoutedEventArgs e)
{
    progressBar.Visibility = Visibility.Collapsed;
}