经过一个复杂的对象到一个页面,而在WP7 Silverlight应用程序导航 [英] Passing a complex object to a page while navigating in a WP7 Silverlight application

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

问题描述

我一直在使用的NavigationService 导航方法来导航到我的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。因此,如果您使用的是查询参数,它会得到自动立碑拿起你的code。任何时候,你会手动传递一个对象的实例,但是,你必须做手工立碑该实例自己。

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页面新牛(){n = 1}你必须确保墓碑和zombificate自己这个值。

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墓碑和zombificate BasePage.Data。

  5. 在的BasePage,我有一次数据属性或者通过Zombification或导航填充时调用的OnDataSet虚方法。在该方法中,具有做业务数据发生的一切。

这篇关于经过一个复杂的对象到一个页面,而在WP7 Silverlight应用程序导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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