2010-11-29 144 views
3

价值传递到另一个页面的XAML可以轻松如何将一个对象从一个xaml页面传递给另一个?

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));

做不过,这只是为字符串值。我想将一个对象传递给xaml页面。我怎么做?

在SO和WP7论坛上发现了类似的问题。解决方案是使用全局变量(不是最好的解决方案)。

WP7: Pass parameter to new page?

http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/81ca8713-809a-4505-8422-000a42c30da8

回答

3

看一看创建的默认密码,当你开始一个新的数据绑定项目。它显示了将选定对象的引用传递给详细信息页面的方法。

+0

谢谢。默认项目通过查询字符串间接传递对象,查询字符串然后从App.ViewModel访问。这将工作,但我希望有一个更直接传递对象的更优雅的解决方案。 – samwize 2010-11-30 03:08:14

1

我建议在看Caliburn.Micro!

http://caliburnmicro.codeplex.com

+0

谢谢我将研究这个框架(听说过几次)。你知道它是否可以传递对象到页面吗? – samwize 2010-11-30 02:59:20

+0

绑定到ViewModel/Presenter/Controller/Screen应该可以管理这个! – 2010-11-30 06:20:07

5

使用OnNavigatedFrom方法当我们调用NavigationService.Navigate方法

OnNavigateFrom被调用。它具有一个NavigationEventArgs对象作为参数,返回目标页面的Content属性,我们可以通过该属性访问目标页面的属性“DestinationPage.xaml.cs”

首先,在目标页面“DestinationPage.xaml。 CS “申报财产 ”SomeProperty“:

public ComplexObject SomeProperty { get; set; } 

现在,在 ”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(); 
    } 
} 

现在,拿在SomeProperty值” DestinationPage。 xaml.cs“:

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); 
} 
相关问题