你好,
可以使用async/await的方式来实现,参考下面示例代码:
private void BtnLoad_Click(object sender, RoutedEventArgs e)
{
// 隐藏 MyImageControl
MyImageControl.Visibility = System.Windows.Visibility.Collapsed;
var imageSource = DownloadImage();
MyImageControl.Source = imageSource.Result;
MyImageControl.Visibility = System.Windows.Visibility.Visible;
}
private async Task<ImageSource> DownloadImage()
{
return await Task.Run(() => {
// 延迟5秒,模拟从网络下载图片延迟效果
Thread.Sleep(3000);
ImageSource img =
new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Children.jpg", UriKind.RelativeOrAbsolute));
return img;
});
}
谢谢!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.