简体   繁体   English

wp7-如何检查图像是否已加载

[英]wp7 - how to check if the image was loaded

I'm trying to bind some data to a wp7 listbox (with custom item template) which includes a thumbnail image for each entry. 我正在尝试将一些数据绑定到wp7列表框(带有自定义项目模板),该列表框包含每个条目的缩略图。 The thing is - I'm running into one problem - when the linked image redirects to a 404 page - I get an empty image as a result and frankly - I have no idea how to check if the loaded data is a proper image or not ... here's the code I'm using right now: 问题是-我遇到了一个问题-当链接的图像重定向到404页面时-结果我得到一个空图像,坦率地说-我不知道如何检查加载的数据是否是正确的图像...这是我现在正在使用的代码:

<ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Grid Height="62"
                                  Width="62">
                                <Image Stretch="UniformToFill"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center">

                                    <Image.Source>
                                        <BitmapImage UriSource="{Binding MiniImage}"
                                                     CreateOptions="DelayCreation, BackgroundCreation" />
                                    </Image.Source>

                                </Image>
                            </Grid>
                            <StackPanel HorizontalAlignment="Center"
                                        VerticalAlignment="Center">
                                <TextBlock Text="{Binding Title}"
                                           Margin="12,4,0,0"
                                           FontSize="26"
                                           FontFamily="Segoe WP Bold" />
                                <TextBlock Text="{Binding PubDate}"
                                           Margin="12,0,0,9"
                                           FontSize="16"
                                           FontStyle="Italic"
                                           Padding="5,0,0,0"
                                           FontFamily="Segoe WP"
                                           Opacity="0.5" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

and here's the code behind: 这是背后的代码:

foreach (var item in RssFeedItems)
        {
            var inputString = item.Description;
            var tempImageList = new List<string>();
            var inputStringBuilt = new StringBuilder(item.Description);
            var temp = 1;

            while (temp > 0)
            {
                var img = inputString.IndexOf("<img", StringComparison.Ordinal);
                var src = inputString.IndexOf("src", img, StringComparison.Ordinal);
                var quot = inputString.IndexOf('"', src + 5);
                var len = quot - (src + 5);
                var sub = len > 0 ? inputString.Substring(src + 5, len) : null;
                tempImageList.Add(sub);
                var closingBracket = inputString.IndexOf(">", src, StringComparison.Ordinal);
                inputStringBuilt.Clear();
                inputStringBuilt.Append(inputString);
                inputStringBuilt.Remove(img, closingBracket - img);
                inputString = inputStringBuilt.ToString();
                temp = inputString.IndexOf("<img", StringComparison.Ordinal);
            }

            item.MiniImage = tempImageList[0] ?? "ApplicationIcon.png";

            var f = tempImageList.IndexOf(null) - 1;
            while (f >= 0)
            {
                PostImages.Add(tempImageList[f]);
                f--;
            }

            tempImageList.Clear();

            FirstListBox.Items.Add(item);

        }

any ideas? 有任何想法吗?

You might want to issue a web request in your code behind, pass the results into PictureDecoder.DecodeJpeg and bind to the WriteableBitmap returned from that instead of the URL. 您可能要在后面的代码中发出Web请求,将结果传递到PictureDecoder.DecodeJpeg并绑定到从中返回的WriteableBitmap而不是URL。 This way you can detect error conditions in the web request and react accordingly. 这样,您可以检测Web请求中的错误情况并做出相应的反应。

The Image element has an event called ImageFailed you will get an exception (contained in the ExceptionRoutedEventArgs) Image元素有一个名为ImageFailed的事件,您将获得一个异常(包含在ExceptionRoutedEventArgs中)

You could use this to detect error when loading images. 您可以使用它来检测加载图像时的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM