【问题标题】:How to pass a list from a xaml page to another? [duplicate]如何将列表从 xaml 页面传递到另一个页面? [复制]
【发布时间】:2014-03-28 23:34:09
【问题描述】:

我正在寻找有关如何在页面之间传递数据的最佳实践。

在页面 A 中,我有一个触发页面 B 的按钮。 在页面 B 我有 6 个文本框,允许用户输入信息。用户完成后,单击按钮将信息存储到列表中,并将它们带回页面 A。

我想将该数据(列表)传递回页面 A。

我正在寻找最佳实践。一般公认的最佳方式是什么?

谢谢

【问题讨论】:

  • 你试过static变量或PhoneApplicationService.Current.State吗?已经有similar question
  • 是的,我见过。但这也适用于列表,以及如何使用?你能举个例子吗,因为我不明白......
  • 我已经添加了示例代码 - 但你有没有尝试过?

标签: windows-phone-7 windows-phone-8


【解决方案1】:

这个问题与this question.完全相同

这是对我最好的答案。

PhoneApplicationService.Current.State["yourparam"] = param
NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative));

然后在其他页面中简单地

var k = PhoneApplicationService.Current.State["yourparam"];

【讨论】:

    【解决方案2】:

    您可以使用PhoneApplicationService.Current.State,它肯定会与 List 一起使用:

    //create Dictionary
    List<int> numbers = new List<int> { 1, 3, 4, 5 };
    
    // add to Dictionary
    PhoneApplicationService.Current.State.Add("test", numbers);
    
    // then change in disctionary if already added
    PhoneApplicationService.Current.State["test"] = numbers;
    
    // retrive from Dictionary
    List<int> newList = PhoneApplicationService.Current.State["test"] as List<int>;
    

    此方法有一个很大的优势 - 您的数据将保存在 Tombstone 状态。

    您可以考虑使用 static 变量(可通过您的命名空间访问。您也可以关注 this article 并通过 NavigationService 传递您的列表 - 我已经在 this answer 中写过一些相关内容。

    【讨论】:

    • 谢谢,我明白了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多