如何从WP7中的RSS文件获取图像和文本?

问题描述:

我正在尝试使用它的图像获取RSS文本,但图像无法显示我将查看获取图像的模型并使用简单的RSS技术获取图像,您会告诉我如何获取图像和文本...... 。 这里是我的XAML代码:如何从WP7中的RSS文件获取图像和文本?

<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> 
+1

你能不能retreive RSS数据,或者你不能显示的数据? – BvdVen

+0

你是什么viewmodel看起来像?,你从rss饲料获得价值? –

您不能绑定到以这种方式作为字符串的URL/URI是不是为了Image.Source属性的有效值。如果在xaml中设置了常量URL,则编译器生成的代码将获取URL并将其转换为BitmapSource,从而图像将正确显示。

要以这种方式绑定图片网址,您将需要converter。该转换器可采取的URL,并将其转换为BitmapImage的:

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}}" />