The documented way to navigate to another XAML page while passing in data seems to be by serializing the data as URL parameters. This works great for simple parameters, such as a single database key:
NavigationService.Navigate(new Uri("/ViewContact.xaml?contactid=" + cid, UriKind.Relative));
However, what if you want to pass in a rather complicated data structure, such as a search query? This query might have keywords, various filters, arrays of items, etc. It would be rather difficult to express this sort of data using URL parameters.
Question: What is the recommended technique to navigate to another page, passing in non-trivial parameters?
One idea would be to first set the data or query as a static property of the page you’ve navigating to:
Query q = new Query();
// Set various parameters
ViewContact.SearchQuery = q;
NavigationService.Navigate(new Uri("/ViewContact.xaml?contactid=", UriKind.Relative));
Then, when ViewContact was loaded, it would check that static property and load the data into memory.
Anything wrong with this approach, or is there another method that’s recommended?
Take a look at this thread on the Prism discussion board: Navigation using Object as parameter.
Near the end, there is a solution using a
NavigationUriclass derived fromUri:(I made the constructor public)
You can then use it like so:
Then, in the
OnNavigatedTomethod of yourViewContact:As suggested in the original thread, you can also use a class to generate the parameter keys instead of hardcoding them: