gpt4 book ai didi

xaml - 如何将对象从 xaml 页面传递到另一个页面?

转载 作者:行者123 更新时间:2023-12-02 04:21:28 25 4
gpt4 key购买 nike

使用

可以轻松地将值传递到另一个 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

最佳答案

使用 OnNavigedFrom 方法

OnNavigateFrom 在我们调用 NavigationService.Navigate 方法时被调用。它有一个 NavigationEventArgs 对象作为参数,返回目标页面及其 Content 属性,我们可以使用该属性访问目标页面“DestinationPage.xaml.cs”的属性

首先,在目标页面“DestinationPage.xaml.cs”中,声明一个属性“SomeProperty”:

public ComplexObject SomeProperty { get; set; }

现在,在“MainPage.xaml.cs”中,重写 OnNavieratedFrom 方法:

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();
}
}

现在,获取“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);
}

关于xaml - 如何将对象从 xaml 页面传递到另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com