Pregunta

I'm trying to save the pushpins on a map to app settings so that they are loaded after the app closes, but I'm not sure how to retrieve the variable from storage. Does anyone know how I would achieve loading the pushpins back onto the map after they are saved?

This is what I have tried so far, I save the pushpins to Isolated Storage after they are added to the map in OnNavigatedTo

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (NavigationContext.QueryString.ContainsKey("GeoLat") && NavigationContext.QueryString.ContainsKey("GeoLong") && NavigationContext.QueryString.ContainsKey("pName"))
            { 
                    var latitude = Convert.ToDouble(NavigationContext.QueryString["GeoLat"]);
                    var longtitude = Convert.ToDouble(NavigationContext.QueryString["GeoLong"]);
                    var MyGeoPosition = new GeoCoordinate(latitude, longtitude);
                    var pushPinName = NavigationContext.QueryString["pName"];

                    DrawPushPin(MyGeoPosition, pushPinName);
                    appSettings.Add("pushPin", MyGeoPosition);      

            }

            base.OnNavigatedTo(e);
        }

And then I try to retrieve the pushpins when the page is loaded like this, but the following errors are thrown. I understand that MyGeoPosition is only seen within OnNavigatedTo so I cant reference it,is there an alternative method of retrieving it? :

private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
        {
            MyMap.MapElements = (var)appSettings[MyGeoPosition];
        }

Error   1   The contextual keyword 'var' may only appear within a local variable declaratio 
Error   2   The name 'MyGeoPosition' does not exist in the current context  
¿Fue útil?

Solución

Since you added the location to appSettings using the key "pushPin" you want to use the same key to retrieve that app setting. You also need to cast it to GeoCoordinate, not 'var'.

private GeoCoordinate MyGeoPosition;    

private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
    MyGeoPosition = (GeoCoordinate)appSettings["pushPin"];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top