简体   繁体   English

在WP8应用程序中的页面之间传递数据

[英]Pass data between pages in WP8 application

I am building a simple Sports Tracker application. 我正在构建一个简单的Sports Tracker应用程序。 I want to start a DispatcherTimer and a GeoCoordinateWatcher at Page 1, and in Page 2 I want a map view. 我想在页面1上启动DispatcherTimerGeoCoordinateWatcher ,在页面2中我想要一个地图视图。 And I am looking for the best practice how to pass the timers and the watchers value between pages, so I can show the updated information on the map. 我正在寻找最佳实践,即如何在页面之间传递计时器和观察者值,以便在地图上显示更新的信息。

I've seen suggestions to save it to an XML or into the IsoStorage. 我看到了将其保存为XML或IsoStorage的建议。 What is the best solution for this problem ? 解决此问题的最佳方法是什么?

Directly you can pass strings between your pages: 您可以直接在页面之间传递字符串:

NavigationService.Navigate(new Uri("/destinationPage.xaml?dataKey="+dataValue.ToString()));

If you want to pass an object the first thing that hits is: 如果要传递对象,首先遇到的是:

NavigationService.Navigate(new Uri("/destinationPage.xaml?dataKey="+dataObject.ToString()));

Here comes the problem: 问题来了:
1) You need to deserialize your object in your destination page. 1)您需要在目标页面中反序列化对象。 Or even worse, 甚至更糟
2) Memory problem. 2)内存问题。 Say you need to pass an image (captured in 41 mega pixel) and you are not allowed to use this much memory: 假设您需要传递一张图像(以41兆像素捕获),并且不允许使用这么多的内存:
So here is another solution: 所以这是另一种解决方案:

PhoneApplicationService.Current.State["yourparam"] = param;

Then you navigate to your destination page and you can access your object: 然后,您导航到目标页面,然后可以访问对象:

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

Note: This thing would write your object in isolated Storage. 注意:这件事会将您的对象写入隔离的存储中。 So may be a bit slow. 所以可能有点慢。
Third option: 第三种选择:
Use a static class to hold your dataValue between your pages. 使用静态类在页面之间保存dataValue。 like: 喜欢:

static class Transporter
{
    public static object container;
}

Now in your source page you can write: 现在,您可以在源页面中编写:

Transporter.container = MyGeoCoordinateWatcherObj;

You can access this in your destination page. 您可以在目标页面中访问它。 (here remains the problem of memory, but in your case you can use it safely.) (这里仍然是内存问题,但您可以安全使用它。)
Note: Sometime microsoft recommends mvvm pattern to pass objects. 注意:有时Microsoft建议使用mvvm模式来传递对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM