【问题标题】:How to open an article of the RSS feed in a new form?如何以新形式打开RSS提要的文章?
【发布时间】:2012-04-22 23:56:23
【问题描述】:

您知道要在代码中添加什么内容以从 RSS 提要打开所需的文章吗?采用新形式。

在一个新的表单中我应该得到文章的标题和内容,图片是可选的

这是我的文章列表所在的代码:

private void ls_text_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
    ListBox listBox = sender as ListBox;

            if (listBox != null && listBox.SelectedItem != null)
            {
                SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;

                if (sItem.Links.Count > 0)
                {
                     if (listBox != null && listBox.SelectedItem != null)
            {

                SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;
                PhoneApplicationService.Current.State["myItem"] = sItem;

                NavigationService.Navigate(new Uri("/Clanak.xaml",UriKind.Relative));// leads to article form

                }
            }
        }
        catch (Exception f)
        {

            MessageBox.Show(f.Message, "", MessageBoxButton.OK);
        }
    }

我已经编写了一个可以完成大部分工作的代码:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        try
        {
            SyndicationItem sItem = PhoneApplicationService.Current.State["myItem"] as SyndicationItem;
            PageTitle.Text = sItem.Title.Text; //Title would go in the pagetitle of the form , Title shows fine
            PageTitle.FontSize = 40;
            //tb_Content.Text = sItem.Summary.Text; //all goes fine

            foreach (SyndicationItem item in sItem.SourceFeed.Items)
            {
                foreach (SyndicationElementExtension ext in item.ElementExtensions)
                {

                    if (ext.GetObject<XElement>().Name.LocalName == "encoded")

                        tb_Content.Text = ext.GetObject<XElement>().Value; //textblock for content, throws NullReferenceException
                }
            }
        }
        catch (Exception f)
        {

            MessageBox.Show(f.Message, "Error clanak", MessageBoxButton.OK);
        }
    }

内容无法识别,我一直得到 NullReference,当我在 TextBlock 上链接摘要时,文章的日期显示正常。此外,每次当我返回列出所有文章的列表时,我都会收到错误消息“您只能在 OnNavigatedTo 和“OnNavigatedFrom”之间使用状态。当我按下主页按钮时,调试器出现(应用程序崩溃)。

这是我得到的: Microsoft.Phone.dll 中出现“System.InvalidOperationException”类型的第一次机会异常 System.Runtime.Serialization.dll 中出现“System.Security.SecurityException”类型的第一次机会异常 mscorlib.dll 中出现了“System.Reflection.TargetInvocationException”类型的第一次机会异常 System.Runtime.Serialization.dll 中出现“System.Security.SecurityException”类型的第一次机会异常 线程 '' (0xfc2037a) 以代码 0 (0x0) 退出。 线程 '' (0xe880366) 以代码 0 (0x0) 退出。 线程 '' (0xe310372) 已退出,代码为 0 (0x0)。 线程 '' (0xf970392) 以代码 0 (0x0) 退出。 线程 '' (0xe470392) 已退出,代码为 0 (0x0)。

这是我正在处理的提要:http://www.zimo.co/feed/ 我的主要问题是如何通过 nullref。异常并获取内容。

【问题讨论】:

  • 目前我正在寻找建议或指点。也许有人以前尝试过这样的事情?

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


【解决方案1】:

首先,您应该将您的Item 保存到某个位置,以便您可以从另一个Page 访问它。

例如:

SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;  
PhoneApplicationService.Current["myItem"] = sItem;

然后,创建一个新页面并导航到它NavigationService.Navigate(new Uri("/newPage.xaml"));

在详情页的构造函数中,根据需要填写标题和内容

SyndicationItem sItem = PhoneApplicationService.Current["myItem"] as SyndicationItem;
// set Title and so on...

【讨论】:

  • 我在“目标”表单中的代码:(我得到 NullReferenceException) private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { try { SyndicationItem sItem = PhoneApplicationService.Current.State["postovi"] as SyndicationItem ; tb_Content.Text = sItem.Content.ToString(); //内容的文本块 PageTitle.Text = sItem.Title.ToString(); //标题将进入表单的页面标题 } catch (Exception f) { MessageBox.Show(f.Message, "Error", MessageBoxButton.OK); } }
  • 请检查,sItemsItem.Contentnull。尝试传递更简单的数据类型,例如 string 以确保一切正常。确保在从此处获取该对象之前将其保存到 State
  • 当我按下主页按钮时应用程序中断:( 可以是我使用的状态吗?
  • 我认为SyndicationItem 不能被序列化。页面导航后将其从State 中删除
  • 我试过了。但这杀死了返回页面的导航。我使用的代码:protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { if (PhoneApplicationService.Current.State.ContainsKey("postovi")) { PhoneApplicationService.Current.State.Remove("postovi"); } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多