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

查看:15
本文介绍了在 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 查询字符串参数而具有完美的墓碑效果.但是,如果您以某种方式向 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. 有一个带有对象类型数据属性的 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 应用程序中导航时将复杂对象传递给页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆