0

您知道要在代码中添加什么内容以从 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。异常并获取内容。

4

1 回答 1

2

首先,您应该将您的文件保存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...
于 2012-04-21T14:07:13.870 回答