0

In my Windows Phone app I fill a longlistselector with data from a web api and by inputting the data to a class using

 private void NewDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error connecting to the server");
        }
        else
        {
            XDocument loadedData = XDocument.Parse(e.Result);

            var data = from query in loadedData.Descendants("pattern")
                       select new newpatterns
                       {
                           ID = (string)query.Element("id"),
                           Title = (string)query.Element("title"),
                           UserName = (string)query.Element("userName"),
                           DateCreated = (string)query.Element("dateCreated"),
                           Url = (string)query.Element("Url"),


                       };
            newlonglist.ItemsSource = data.ToList();

        }
    }

then when the an item is selected in the long list selector I navigate to a second page. using

private void newlonglist_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LongListSelector selector = sender as LongListSelector;

        if (selector == null)
            return;

        newpatterns newdata = selector.SelectedItem as newpatterns;

        if (newdata == null)
            return;


        //MessageBox.Show(newdata.Hex);
        NavigationService.Navigate(new Uri("/NewImagePage.xaml?id=" + newdata.ID, UriKind.Relative));
        newlonglist.SelectedItem = null;

    }

My problem is then in on the secondpage "NewImagePage.xaml" I want to find find this selected item in the call from the ID so I can get the rest of the data, is there a way to do this with out passing it all as a query string..

Thanks..

opalenzuela
  • 3,139
  • 21
  • 41
Kasper S Mathiesen
  • 669
  • 1
  • 7
  • 17

2 Answers2

1

Yes there are different ways, you can for example:

  • follow Zik answer and extend your NavigationService, works quite nice, but has disadvantage that your parameter/object get lost when your App goes into TombStone state, (also you can find it here),
  • better would be to use AMR Solution - the data would then persist. (also look at other answers to the question that AMR posted, there are also some ways described),
  • you can also try to use IsolatedStorage for this, but personally I think the second option would be better.

Basically everything depends on how your code looks and what you want to achieve.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
1

You can use IsolatedStorageSettings feature of windows phone to do your work. Here is the solution of your problem.

private void newlonglist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
      IsolatedStorageSettings MemorySettings = IsolatedStorageSettings.ApplicationSettings;
        LongListSelector selector = sender as LongListSelector;
        if (selector == null)
            return;
        newpatterns newdata = selector.SelectedItem as newpatterns;
        if (newdata == null)
            return;
        //MessageBox.Show(newdata.Hex);
        MemorySettings.Add("SelectedItem",newdata );
        NavigationService.Navigate(new Uri("/NewImagePage.xaml, UriKind.Relative));        
    }

In NewImagePage.xaml

protected override void OnNavigatedTo(NavigationEventArgs e)
     { IsolatedStorageSettings MemorySettings = IsolatedStorageSettings.ApplicationSettings;
         if(MemorySettings.Contains("SelectedItem"))
            {
               newpatterns newdata = MemorySettings["SelectedItem"] as newpatterns ;
               MemorySettings.Remove("SelectedItem");
            }
     }
Jaihind
  • 2,770
  • 1
  • 12
  • 19
  • @KasperSMathiesen and Jaihind - only remember to handle what you have saved in ISS properly. Watch out when you use ISS somewhere else and SaveIt. It depends on your code if the key should persist (or already is there - then can be a problem and exception when you add duplicate). – Romasz Feb 04 '14 at 11:27