我有一个跟踪用户位置的 WP7 应用程序。一切正常,除了我想在 GeoCoordinate 观察者的位置更改事件上将位置写入隔离存储,并且我不断收到“Operation Not Permitted on IsolatedStorageFileStream”消息。任何人都可以帮助让它工作吗?
将坐标保存到文件的成员是:
let xname n = XName.op_Implicit(n)
let xdoc (el: seq<XElement>) = new XDocument(Array.map box (Array.ofSeq el))
let xelem s el = new XElement(xname s, box el)
let xstr s = box s
member this.createLocationsFile latitude longitude =
try
let doc : XDocument =
xdoc
[xelem "root"
[xelem "location"
[(xelem "latitude" (xstr latitude))
(xelem "longitude" (xstr longitude))
]
]
]
use store = IsolatedStorageFile.GetUserStoreForApplication()
if not (store.FileExists("locations.xml")) then
let file = new IsolatedStorageFileStream("locations.xml", IO.FileMode.Create, store)
doc.Save(file)
else
let file = new IsolatedStorageFileStream("locations.xml", IO.FileMode.Open, store)
let docAmended : XDocument = XDocument.Load(file)
let elementToAdd =
docAmended.Element(xname "root").Add(
[xelem "location"
[(xelem "latitude" (xstr latitude))
(xelem "longitude" (xstr longitude))
]
])
docAmended.Save(file)
with
| :? IsolatedStorageException as ex -> MessageBox.Show("Error saving file: " + ex.Message) |> ignore
| _ -> MessageBox.Show("Unable to open file") |> ignore
PositionChangedEventHandler 是:
let MyPositionChanged(e : GeoPositionChangedEventArgs<GeoCoordinate>, map : Map, ellipse : Ellipse) =
let ppLoc = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude)
map.SetView(ppLoc, 10.0)
//do layer.AddChild(ellipse, ppLoc)
ellipse.Visibility <- System.Windows.Visibility.Visible
let iso = new IsolatedStorageHelper()
let lat = ppLoc.Latitude.ToString()
let lon = ppLoc.Longitude.ToString()
do iso.createLocationsFile lat lon