4

I'm using the NavigationContext.QueryString for my Windows Phone 8 app. For example, I set an URI identifier like ItemId in the navigation string and in the OnNavigatedTo, I parse the Id and read the Item via linq.

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        try
        {
            int itemId = int.Parse(NavigationContext.QueryString["itemId"]);

            _item = App.MainViewModel.GetItem(itemId);

            DataContext = _item;
        }
        catch (KeyNotFoundException ex)
        {
            Debug.WriteLine(ex.Message);
            throw;
        }
    }

I've found an interesting alternative and want to hear your opinion:

// in the calling page
PhoneApplicationService.Current.State["Item"] = App.MainViewModel.GetItem(123);

// in the destination page
Item item = PhoneApplicationService.Current.State["Item"] as Item;

Is this really a recommended way?

zirkelc
  • 1,451
  • 1
  • 23
  • 49

1 Answers1

0

From MSDN:

The PhoneApplicationService class provides access to various aspects of the application’s lifetime. This includes management of the application’s idle behavior and management of the application’s state when it becomes active or inactive. You can use it like that but the data has to be serializable.

This link has other ways to share data, but I don't think state is the recommended way of doing it. It's more for tombstoning purpose.

Community
  • 1
  • 1
AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • If you quote MSDN, add a reference to it. – Den Jan 10 '13 at 20:28
  • Is my current solution the better way? I'm not sure, because I already have the objects before navigating, but I need to read them in every page. – zirkelc Jan 11 '13 at 08:17
  • @websocialist I believe your solution is more flexible, as it allows you to have multiple instances of the same page in the navigation stack. If you used state, when you navigate to a new instance of the page with a new id, the id would be replaced in the previous instances as well, since the state is shared. And your solution allows you to easily pin the page if needed. – Kevin Gosse Jan 11 '13 at 09:42
  • That's a good arguement! But whats about the way I retrieve the item from the MainViewModel. My current solution is a simple GedItem method which throws an exception if the Item is not founded. Maybe a TryGetItem method is the better solution? – zirkelc Jan 11 '13 at 10:38