How do I pass non-string parameters between pages in windows phone 8?

Mr. Spoof picture Mr. Spoof · Nov 30, 2012 · Viewed 7.5k times · Source

I'm working on converting a windows store app to windows phone 8. For WinRT, you could pass any object as a parameter when calling frame.navigate. (frame.navigate(type sourcePageType, object parameter))

The navigation seems to work differently for windows phone, you navigate by calling into a uri, like: frame.navigate(new uri("mypage.xaml", UriKind.Relative))

The windows documentation notes that you can pass a string as a parameter by adding it to the uri.

Is there an accepted way of passing complex objects between pages that I just haven't found?

Answer

Zik picture Zik · Dec 23, 2012

I ended up extending the NavigationService class, like so:

public static class NavigationExtensions
{
    private static object _navigationData = null;

    public static void Navigate(this NavigationService service, string page, object data)
    {
        _navigationData = data;
        service.Navigate(new Uri(page, UriKind.Relative));
    }

    public static object GetLastNavigationData(this NavigationService service)
    {
        object data = _navigationData;
        _navigationData = null;
        return data;
    }
}

Then you'd call NavigationService.Navigate("mypage.xaml", myParameter); on the source page, and in the OnNavigatedTo method of the target page call var myParameter = NavigationService.GetLastNavigationData(); to get the parameter data.