简体   繁体   中英

Pass data between pages in WP8 application

I am building a simple Sports Tracker application. I want to start a DispatcherTimer and a GeoCoordinateWatcher at Page 1, and in Page 2 I want a map view. 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. 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. Or even worse,
2) Memory problem. Say you need to pass an image (captured in 41 mega pixel) and you are not allowed to use this much memory:
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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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