传递参数的方法
1.使用查询字符串
您可以通过查询字符串传递参数,使用此方法意味着必须将数据转换为字符串并对其进行url编码。您只应使用它来传递简单数据。
导航页面:
page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));目标页面:
string parameter = string.Empty;if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) { this.label.Text = parameter;}2.使用NavigationEventArgs
导航页面:
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.."; }}目标页面:
// Just use the value of "PublicProperty"..
3.使用手动导航
导航页面:
page.NavigationService.Navigate(new Page("passing a string to the constructor"));目标页面:
public Page(string value) { // Use the value in the constructor...}Uri和手动导航之间的区别
我认为这里的主要区别是应用程序生命周期。出于导航原因,手动创建的页面会保留在内存中。在此处了解更多信息。
传递复杂的对象
您可以使用方法一或二来传递复杂的对象(推荐)。您还可以将自定义属性添加到
Application类或将数据存储在中
Application.Current.Properties。



