【问题标题】:get the url <img src=''> a rss feed and open on <image> in xaml Windos Phone获取 url <img src=''> 一个 rss 提要并在 xaml Windows Phone 中的 <image> 上打开
【发布时间】:2025-12-19 19:05:11
【问题描述】:

我有这门课:

class clsFeed
    {
        public string Title { get; set; }
        public string Summary { get; set; }
        public string PublishDate { get; set; }
        public string Content { get; set; }
        public string Image { get; set; }
        public Uri Link { get; set; }
    }

我需要获取位于标签http://www.unnu.com/feed 内的标签,并在列表中的主页上打开此地址以及其他数据。这是我正在使用的代码。它可以编译,但在我看来他一直是空的。

private void UpdateFeedList(string feedXML)
        {
            // Load the feed into a SyndicationFeed instance
            StringReader stringReader = new StringReader(feedXML);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

            //var counter = feed.Items.Count();
            var lista = feed.Items.ToList().Select(s => new clsFeed
            {
                Title = s.Title.Text,
                Summary = s.Summary.Text,
                PublishDate = s.PublishDate.Date.ToString(),
                Content = "",
                Imagem = s.Summary.Text.Split('<')[5].Replace("img src='", "").Replace("' border='0' />", ""),
                Link = s.Links.FirstOrDefault().Uri
            }).ToList();


                      Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Bind the list of SyndicationItems to our ListBox
                feedListBox.ItemsSource = lista;
                feedListBox.ItemsSource = feed.Items;
                //progressIndicator.IsVisible = false;
            });
            //gridProgressBar.Visibility = Visibility.Collapsed;   //Quando atualiza a lista de feeds esconde a progress bar.
            panorama.Visibility = Visibility.Visible;
        }

private void UpdateFeedList(string feedXML)
    {
        // Load the feed into a SyndicationFeed instance
        StringReader stringReader = new StringReader(feedXML);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        SyndicationFeed feed = SyndicationFeed.Load(xmlReader);



        // In Windows Phone OS 7.1, WebClient events are raised on the same type of thread they were called upon. 
        // For example, if WebClient was run on a background thread, the event would be raised on the background thread. 
        // While WebClient can raise an event on the UI thread if called from the UI thread, a best practice is to always 
        // use the Dispatcher to update the UI. This keeps the UI thread free from heavy processing.
        var lista = feed.Items.ToList().Select(s => new clsFeed
        {
            Title = s.Title.Text,
            Summary = s.Summary.Text,
            PublishDate = s.PublishDate.Date.ToString(),
            Content = "",                
            Imagem = Regex.Match(feedXML, @"<img\s+src='(.+)'\s+border='0'\s+/>").Groups[1].Value,
            Link = s.Links.FirstOrDefault().Uri
        }).ToList();




        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // Bind the list of SyndicationItems to our ListBox
            feedListBox.ItemsSource = lista;


        });

        gridProgressBar.Visibility = Visibility.Collapsed;   //Quando atualiza a lista de feeds esconde a progress bar.
        panorama.Visibility = Visibility.Visible;
    }

这可行,但屏幕上没有显示任何内容。有谁知道为什么?

我的列表框保持不变。


没有任何错误。

<ListBox x:Name="feedListBox" HorizontalAlignment="Left" Height="537" Margin="10,72,0,0" VerticalAlignment="Top" Width="398" SelectionChanged="listFeedBox_SelectionChanged" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel VerticalAlignment="Top">
                                    <Grid>
                                    <Image Width="400" Height="130" Name="img" Source="{Binding feed.ImageUrl}"/>
                                    </Grid>
                                    <TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" >
                                        <TextBlock.Foreground>
                                            <SolidColorBrush Color="#FF159DDE"/>
                                        </TextBlock.Foreground>
                                    </TextBlock>
                                    <TextBlock Name="feedSummary" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" />
                                    <TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate}" />
                                    <TextBlock Name="feedContent" Text="{Binding Content}" />
                                </StackPanel>
                           </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

【问题讨论】:

  • 什么是空的?我已经测试了你的代码,它返回了 50 个元素,所以“lista”很好。如果问题出在 ListBox 上,它是如何定义的?
  • 图片没有出现
  • 为什么是 {Binding feed.ImageUrl} 而不是 {Binding Image}?无论如何,SL 似乎不能很好地处理图像 URL,它需要一些帮助:vivekdalvi.wordpress.com/2008/03/30/binding-to-image-source 这真的很奇怪,这样一个基本的东西是无法相信的 :( 尝试解决方法并给我们您的反馈... (也见这里:nullskull.com/faq/8/…
  • 绑定没有发现类读取有问题...例如,检查对象的属性是否带有消息框。完美运行。但问题仍然存在。
  • 您是否尝试过两个链接中提供的解决方法?

标签: c# windows-phone-7 rss feed


【解决方案1】:

您解析图像 URL 的启发式方法很脆弱,您应该使用正则表达式:

using System.Text.RegularExpressions;
...
Image = Regex.Match(s.Summary.Text, @"<img\s+src='(.+)'\s+border='0'\s+/>").Groups[1].Value,

【讨论】:

  • 我的图片代码是: 此代码不在屏幕上显示图片... =/
  • 不寻找类的任何字段,没有图像,没有链接或任何东西。
  • 我猜“Imagem”而不是“Image”只是您评论中的一个错字。此外,“feedListBox.ItemsSource = feed.Items”行的注释是否正确?
  • 我刚刚更改了图片,让您了解为什么图片是葡萄牙语。我设法用你所说的打开了图像,但没有出现在屏幕上。如果我触摸屏幕的黑暗部分,它会打开我想要的内容。但我看不到这些项目。注释部分不是我们需要的。此评论是 SyndicationFeed 的一部分,它不采用此类创建的图像。获取所有图像属性等。
  • 我的意思是“Imagem”与您使用“Image”的帖子顶部的类定义不一致:) 无论如何我从未使用过 W7 的东西,但我猜在 XAML 中你添加一些“图像”控件,其中“源”属性设置为 WPF 或 Silverlight 中的 URL。您的 ListBox 的 XAML 模板是什么样的?