我正在尝试将一些数据绑定到 wp7 列表框(带有自定义项目模板),其中包含每个条目的缩略图。问题是 - 我遇到了一个问题 - 当链接的图像重定向到 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>
这是背后的代码:
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);
}
有任何想法吗?