简体   繁体   中英

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. 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:

<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. This way you can detect error conditions in the web request and react accordingly.

The Image element has an event called ImageFailed you will get an exception (contained in the ExceptionRoutedEventArgs)

You could use this to detect error when loading images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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