0

Do you know what to add to the code to open desired article from the RSS feed. In a new form.

In a new form I should get Title and Content of the article, Image is optional

Here is my code where the list of articles is:

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);
        }
    }

I have written a code that does most of the job right:

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);
        }
    }

The content isnt recognized and I get NullReference all the time, when I linked Summary on the TextBlock the date of the article was shown fine. Also every time when I goback on list where all the articles are listed I get an Error "You can only use State between OnNavigatedTo" and "OnNavigatedFrom". When I press home button debugger shows up (app crashes).

This is what I get: A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.dll A first chance exception of type 'System.Security.SecurityException' occurred in System.Runtime.Serialization.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll A first chance exception of type 'System.Security.SecurityException' occurred in System.Runtime.Serialization.dll The thread '' (0xfc2037a) has exited with code 0 (0x0). The thread '' (0xe880366) has exited with code 0 (0x0). The thread '' (0xe310372) has exited with code 0 (0x0). The thread '' (0xf970392) has exited with code 0 (0x0). The thread '' (0xe470392) has exited with code 0 (0x0).

This is the feed I am working on: http://www.zimo.co/feed/ my main problem is how to get past the nullref. exception and get the content.

Goran303
  • 591
  • 1
  • 6
  • 16
  • currently I am looking for suggestions or pointers. Maybe somebody tried something like this before? – Goran303 Apr 21 '12 at 13:05

1 Answers1

2

First of all you should save your Item into some place, where you have access it from another Page.

For example:

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

Than, create a new page and navigate to it NavigationService.Navigate(new Uri("/newPage.xaml"));

In constructor of details page, fill title and content as you need

SyndicationItem sItem = PhoneApplicationService.Current["myItem"] as SyndicationItem;
// set Title and so on...
Ku6opr
  • 8,126
  • 2
  • 24
  • 31
  • my code at the "destination" form: (I get NullReferenceException) private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { try { SyndicationItem sItem = PhoneApplicationService.Current.State["postovi"] as SyndicationItem; tb_Content.Text = sItem.Content.ToString(); //textblock for content PageTitle.Text = sItem.Title.ToString(); //Title would go in the pagetitle of the form } catch (Exception f) { MessageBox.Show(f.Message, "Error", MessageBoxButton.OK); } } – Goran303 Apr 21 '12 at 18:38
  • Please check, `sItem` or `sItem.Content` is `null`. Try to pass more simpler data types such a `string` to make sure that all works. Make sure that object is saved into a `State` before you get it from here. – Ku6opr Apr 21 '12 at 19:29
  • App breaks when I push the home button :( Can it be the State I use ? – Goran303 Apr 23 '12 at 09:22
  • I think `SyndicationItem` can't be serialized. Remove it from `State` after page navigation – Ku6opr Apr 23 '12 at 10:48
  • I tried. but that killed the back navigation to the page. code I used: protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { if (PhoneApplicationService.Current.State.ContainsKey("postovi")) { PhoneApplicationService.Current.State.Remove("postovi"); } } – Goran303 Apr 23 '12 at 15:57
  • fixed it, so far no errors. code I used: IsolatedStorageSettings.ApplicationSettings["postovi"] = sItem; // to set state SyndicationItem sItem = IsolatedStorageSettings.ApplicationSettings["postovi"] as SyndicationItem; // to get state in article page //to remove from state protected override void OnNavigatedFrom (System.Windows.Navigation.NavigationEventArgs e) { if (PhoneApplicationService.Current.State.ContainsKey("postovi")) { PhoneApplicationService.Current.State.Remove("postovi");} } still I cant get content only summary :( – Goran303 Apr 23 '12 at 16:25
  • Sourcefeed not carried over from list of articles (where is SyndicationFeed) to Article form. – Goran303 Apr 24 '12 at 11:05