传递参数的方法
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));
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Page destinationPage = e.Content as Page;
if (destinationPage != null) {
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) {
}
Uri和手动导航之间的区别
我认为这里的主要区别在于应用程序的生命周期。手动创建的页面将因导航原因而保留在内存中。了解更多信息,请阅读此处。
传递复杂对象
您可以使用方法一或方法二来传递复杂对象(建议使用)。您还可以向Application
类添加自定义属性或将数据存储在Application.Current.Properties
中。
NavigationContext.QueryString.TryGetValue("parameter", out parameter)
需要在以下方法内被调用:protected override void OnNavigatedTo(NavigationEventArgs e)
。 - Vishaltypeof
运算符的导航怎么样?你没有写任何关于它的内容。 - fnc12