sam*_*ize 3 xaml windows-phone-7
将值传递到另一个xaml页面可以轻松完成
NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));
但这仅适用于字符串值.我想将一个对象传递给xaml页面.我怎么做?
在SO和WP7论坛上发现了类似的问题.解决方案是使用全局变量(不是最好的解决方案).
使用OnNavigatedFrom方法
当我们调用NavigationService.Navigate方法时调用OnNavigateFrom.它有一个NavigationEventArgs对象作为参数,返回目标页面及其Content属性,我们可以使用它来访问目标页面"DestinationPage.xaml.cs"的属性
首先,在目标页面"DestinationPage.xaml.cs"中,声明属性"SomeProperty":
public ComplexObject SomeProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
现在,在"MainPage.xaml.cs"中,重写OnNavigatedFrom方法:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// NavigationEventArgs returns destination page "DestinationPage"
DestinationPage dPage = e.Content as DestinationPage;
if (dPage != null)
{
// Change property of destination page
dPage.SomeProperty = new ComplexObject();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在"DestinationPage.xaml.cs"中获取SomeProperty值:
private void DestinationPage_Loaded(object sender, RoutedEventArgs e)
{
// This will display a the Name of you object (assuming it has a Name property)
MessageBox.Show(this.SomeProperty.Name);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7802 次 |
| 最近记录: |