问题
I have a GeoCoordinate list that I want to save to storage when the application is closed, but I'm not sure how to save it to storage.
I tried saving the list using a helper class found here problem Storing a list of Objects in Isolated Storage but I think my syntax may be wrong in saving it as I'm new to using lists.This is how I tried to save the list. Can anyone point me in the right direction with saving the lit?
mycoord = Isolated_Storage_Helper.IsoStoreHelper
.SaveList<mycoord>("Storage_Folder/", "Storage");
It gives me an error stating that mycoord is a field but is used as a type
mycoord
is a list of coordinates created at a global level:
List<GeoCoordinate> mycoord = new List<GeoCoordinate>();
And populated in the OnNavigatedTo method:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("GeoLat") &&
NavigationContext.QueryString.ContainsKey("GeoLong") &&
NavigationContext.QueryString.ContainsKey("pName"))
{
if (mycoord.Count >= 2)
{
//do something,draw route between points
return;
}
else
{
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);
mycoord.Add(MyGeoPosition);
}
}
base.OnNavigatedTo(e);
}
回答1:
First create the coordinate class:
[DataContract]
class coord{
[DataMember]
public double lat{get;set;}
[DataMember]
public double lon{get;set;}
}
Now if you want to save a List<coord>
You may do:
DataContractSerializer ser = new DataContractSerializer(typeof(List<coord>));
ser.WriteObject(any_file_stream, instance_of_List<coord>);
But if you want to save just a couple of coordinates, not a list IsolatedStorageSetting would be a better option.
回答2:
First create a cusom serializable object
[DataContractAttribute]
public class GeoPostionObj{
[DataMember]
Double lat {get;set;}
[DataMember]
Double lon {get; set;}
public GeoPositionObj(double lat, double lon){
this.lat = lat;
this.lon = lon;
}
}
Now you can initialize and save this object
//Where you want to save it
//We assume you already have your lat and lon retrieved
GeoPositionObj saveObj = new GeoPositionObj(lat,lon);
EZ_Iso.IsolatedStorageAccess.SaveFile("GeoPos",saveObj);
//Then to retrieve
GeoPositionObj retrievedObj = (GeoPositionObj)EZ_Iso.IsolatedStorageAccess.GetFile("GeoPos",typeof(GeoPositionObj));
You can find the EZ_Iso.dll here http://anthonyrussell.info/postpage.php?name=2
The DLL is free and includes documentation and source code if you are interested in how it works.
Let me know if you have any issues
来源:https://stackoverflow.com/questions/22563502/how-to-save-a-list-of-coordinates-to-isolated-storage