在 WP7 Silverlight 应用程序中导航时将复杂对象传递

Passing a complex object to a page while navigating in a WP7 Silverlight application(在 WP7 Silverlight 应用程序中导航时将复杂对象传递到页面)

本文介绍了在 WP7 Silverlight 应用程序中导航时将复杂对象传递到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 NavigationServiceNavigate 方法导航到我的 WP7 Silverlight 应用程序中的其他页面:

I have been using the NavigationService's Navigate method to navigate to other pages in my WP7 Silverlight app:

NavigationService.Navigate(new Uri("/Somepage.xaml?val=dreas", UriKind.Relative));

Somepage.xaml,然后我检索查询字符串参数如下:

From Somepage.xaml, I then retrieve the query string parameters as follows:

string val;
NavigationContext.QueryString.TryGetValue("val", out val);

<小时>

我现在需要一种使用类似方式传递复杂对象的方法.每次我需要将对象传递到新页面时,如何不必序列化对象?


I now need a way to pass a complex object using a similar manner. How can I do this without having to serialize the object every time I need to pass it to a new page?

推荐答案

这是一个极其复杂的问题,这里没有简单的解决方案.没有任何神奇的 API 可以适用于任何应用来解决这个问题.

This is an extremely complex problem, and there is no simple solution here. There is no magic API that would just work for any app to solve this problem.

传递导航数据的核心问题是墓碑.默认情况下唯一被墓碑化的数据是导航 URI.因此,如果您使用的是 QueryString 参数,它将被墓碑和您的代码自动拾取.但是,任何时候您手动传递对象的实例时,您都必须自己手动为该实例执行墓碑.

The core problem with passing navigation data is Tombstoning. The only piece of data that is tombstoned by default is the Navigation URI. so if you're using a QueryString parameter, it'll get picked up automatically by tombstoning and your code. Any time you'll manually pass a instance of an object though, you'll have to manually do tombstoning for that instance yourself.

因此,如果您导航到/CowDetails.xaml?ID=1",您的页面可能只需选择 ID Querystring 参数即可拥有完美的墓碑.但是,如果您以某种方式为 CowDetails 页面提供new Cow() { ID = 1}",则必须确保自己墓碑化并僵尸化此值.

So, if you navigate to "/CowDetails.xaml?ID=1" your page will probably have perfect tombstoning just by picking up on the ID Querystring Parameter. However, if you somehow provide CowDetails page with a "new Cow() { ID = 1}" you'll have to make sure to tombstone and zombificate this value yourself.

还有时间问题.在调用 NavigationService.Navigate 时,您正在导航的页面还没有实际实例.因此,即使您正在导航到 FooPage 并准备好 FooData,也无法立即将 FooPage 连接到 FooData.您必须等到 PhoneApplicationFrame.Navigated 事件触发后才能为 FooPage 提供 FooData.

Also, there's the issue of timing. While calling NavigationService.Navigate, the page you're navigating too doesn't have an actual instance yet. So even though you're navigating to FooPage and have the FooData ready, there's no way to immediately connect FooPage to FooData. You'll have to wait until the PhoneApplicationFrame.Navigated event has fired in order to provide FooPage with FooData.

我通常处理这个问题的方式是:

The way I normally deal with this is problem:

  1. 拥有一个带有 Object 类型 Data 属性的 BasePage
  2. 让 NavigationHelper 获取页面 URI 和数据:NavigationHelper.Navigate("foo.xaml", fooData)
  3. 让 NavigationHelper 注册到 PhoneApplicationFrame.Navigated 事件,如果它是foo.xaml",则将 BasePage.Data 设置为 FooData.
  4. 让 BasePage 使用 JSON.Net 墓碑和僵尸化 BasePage.Data.
  5. 在 BasePage 上,我有一个 OnDataSet 虚拟方法,一旦通过 Zombification 或 Navigation 填充 Data 属性,就会调用该方法.在这种方法中,与业务数据有关的一切都会发生.

这篇关于在 WP7 Silverlight 应用程序中导航时将复杂对象传递到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 WP7 Silverlight 应用程序中导航时将复杂对象传递

基础教程推荐