Methods to pass parameters
1. Using the query string
You can pass parameters through the query string, using this method means have to convert your data to strings and url encode them. You should only use this to pass simple data.
Navigating page:
page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));
Destination page:
string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) {
this.label.Text = parameter;
}
2. Using NavigationEventArgs
Navigating page:
page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));
// and ..
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// NavigationEventArgs returns destination page
Page destinationPage = e.Content as Page;
if (destinationPage != null) {
// Change property of destination page
destinationPage.PublicProperty = "String or object..";
}
}
Destination page:
// Just use the value of "PublicProperty"..
3. Using Manual navigation
Navigating page:
page.NavigationService.Navigate(new Page("passing a string to the constructor"));
Destination page:
public Page(string value) {
// Use the value in the constructor...
}
Difference between Uri and manual navigation
I think the main difference here is the application life cycle. Pages created manually are kept in memory for navigation reasons. Read more about it here.
Passing complex objects
You can use method one or two to pass complex objects (recommended). You can also add custom properties to the Application
class or store data in Application.Current.Properties
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…