【问题标题】:Loading image from URL in Windows Phone 7在 Windows Phone 7 中从 URL 加载图像
【发布时间】:2011-11-16 22:12:50
【问题描述】:

我使用下面的代码从我的 winodws phone 7 应用程序中的 URL 加载图像。

Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);

它对我来说很好用。但是图像是异步加载的,当我想在那里显示某种忙碌指示符时,如果此类 URL 上不存在图像,那么我想显示一些默认图像。我怎样才能做到这一点?

【问题讨论】:

    标签: silverlight windows-phone-7 image-loading


    【解决方案1】:

    我认为如果您订阅了Image.ImageFailed Event,您应该能够在图像不存在的情况下显示为默认图像。

    可能发生此事件的条件包括:

    1. 找不到文件。
    2. 文件格式无效(无法识别或不受支持)。
    3. 上传后出现未知文件格式解码错误。

    所以这样的事情可能对你有用:

    image1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
    Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
    image1.Source = new BitmapImage(uri);
    
    void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
         // Show the default image
    }
    

    【讨论】:

    • 如果图像存在,那么我想在从 URL 加载时显示一些进度条或忙碌指示符。你能提供它的示例代码吗?
    • 这个概念没问题,但如果图像存在但加载需要时间怎么办。我想同时在那里显示指标。
    • 我认为你应该在这两种情况下都显示忙碌指示符(如果图像存在或不存在)。我现在没有代码示例,但请探索BusyIndicator control here
    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 2012-02-17
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多