我正在尝试将地图上的图钉保存到应用设置,以便在应用关闭后加载它们,但我不确定如何从存储中检索变量。有人知道如何在保存后将图钉加载到地图上吗?
这是我到目前为止所尝试的,我将图钉在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);
}
然后我尝试在页面加载时检索图钉,但会引发以下错误。我知道MyGeoPosition
只能在OnNavigatedTo中看到,所以我无法引用它,是否有另一种检索方法? :
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
答案 0 :(得分:3)
由于您使用密钥" pushPin"将位置添加到appSettings您想使用相同的密钥来检索该应用设置。您还需要将其强制转换为GeoCoordinate,而不是' var'。
private GeoCoordinate MyGeoPosition;
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
MyGeoPosition = (GeoCoordinate)appSettings["pushPin"];
}