2012-04-21 80 views
0

您是否知道要添加到代碼中以便從RSS提要中打開所需的文章?以新的形式。如何以新形式打開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); 
     } 
    } 

內容無法識別,我得到NullReferen一直以來,當我在TextBlock上鍊接摘要時,文章的日期顯示正常。同樣每當我在列表中列出所有文章的列表時,我都會收到錯誤「您只能在OnNavigatedTo之間使用狀態」和「OnNavigatedFrom」。當我按Home按鈕調試器顯示(應用程序崩潰)。

這就是我得到: 「System.InvalidOperationException」類型的第一次機會異常出現在Microsoft.Phone.dll 類型「System.Security.SecurityException」的第一次機會異常出現在System.Runtime.Serialization .dll 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。例外並獲取內容。

+0

目前我填寫標題和內容我正在尋找建議或指針。 也許有人嘗試過這樣的事情? – Goran303 2012-04-21 13:05:51

回答

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... 
+0

我的代碼在 「目的地」 的形式:(I得到的NullReferenceException) 私人無效PhoneApplicationPage_Loaded(對象發件人,RoutedEventArgs E) { 嘗試 {SyndicationItem sItem = PhoneApplicationService.Current.State [ 「postovi」]作爲SyndicationItem; tb_Content.Text = sItem.Content.ToString(); // textblock for content PageTitle.Text = sItem.Title.ToString(); //標題將以 } 捕捉(例外f) { MessageBox。顯示(f.Message,「Error」,MessageBoxButton.OK); } } – Goran303 2012-04-21 18:38:39

+0

請檢查'sItem'或'sItem.Content'是否爲'null'。嘗試傳遞更簡單的數據類型,比如'string'來確保所有的工作。確保對象在從這裏獲得之前保存到「State」中。 – Ku6opr 2012-04-21 19:29:54

+0

字符串通過,好的。 – Goran303 2012-04-21 22:49:59