如何从WP7中的RSS文件中获取图像和文本
本文关键字:获取 图像 文本 文件 RSS WP7 中的 | 更新日期: 2023-09-27 18:37:13
我正在尝试获取带有其图像的 RSS 文本,但图像无法显示,我将查看用于获取图像的模型并使用简单的 RSS 技术获取图像,请告诉我如何获取图像和文本......这是我的 XAML 代码:
<ListBox Name="lstRSS" ItemsSource="{Binding FeedItems}" DataContext="{StaticResource MainViewModel}" FontSize="30" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="700">
<TextBlock Text="{Binding Path=Title}"></TextBlock>
<UserControls:Loader Width="100" Height="100" />
<Image Source="{Binding Link}" Width="450" Height="350" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
不能以这种方式绑定到 URL,因为 String/Uri 不是 Image.Source 属性的有效值。如果在 xaml 中设置了常量 URL,则当编译器生成的代码获取 URL 并将其转换为位图源时,图像将正确显示。
要以这种方式绑定图像 URL,您需要一个转换器。转换器可以获取 URL 并将其转换为位图图像:
public class UriToImageConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// This could be extended to accept a Uri as well
string url = value as string;
if (!string.IsNullOrEmpty(url))
{
return new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
然后,需要将此类的实例添加到应用资源(在 app.xaml 或页面 xaml 中):
<local:UriToImageConverter x:Key="ImageConverter"/>
然后,可以像这样设置绑定:
<Image Source="{Binding Link, Converter={StaticResource ImageConverter}}" />