F#windows phone独立存储和XML

F#windows phone独立存储和XML,xml,windows-phone-7,f#,isolatedstoragefile,Xml,Windows Phone 7,F#,Isolatedstoragefile,我有一个跟踪用户位置的WP7应用程序。一切正常,除了我想在GeoCoordination watcher的位置更改事件中将位置写入隔离存储,并且我一直收到“不允许对隔离存储文件流执行操作”消息。有人能帮我把这件事做好吗 要将坐标保存到文件的成员是: let xname n = XName.op_Implicit(n) let xdoc (el: seq<XElement>) = new XDocument(Array.map box (Array.ofSeq el)) let xel

我有一个跟踪用户位置的WP7应用程序。一切正常,除了我想在GeoCoordination watcher的位置更改事件中将位置写入隔离存储,并且我一直收到“不允许对隔离存储文件流执行操作”消息。有人能帮我把这件事做好吗

要将坐标保存到文件的成员是:

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
让xname n=xname.op_隐式(n)
设xdoc(el:seq)=新XDocument(Array.map框(Array.ofSeq el))
设xelem s el=新的XElement(xname s,框el)
设xstr s=box s
成员this.createLocationsFile纬度经度=
尝试
让医生:XDocument=
xdoc
[xelem“根”
[xelem“位置”
[(xelem“纬度”(xstr纬度))
(xelem“经度”(xstr经度))
]
]
]                    
使用store=IsolatedStorageFile.GetUserStoreForApplication()命令
如果没有(store.FileExists(“locations.xml”)),则
让file=new-IsolatedStorageFileStream(“locations.xml”,IO.FileMode.Create,store)
文件保存(文件)
其他的
让file=new-IsolatedStorageFileStream(“locations.xml”,IO.FileMode.Open,store)
让文档修改:XDocument=XDocument.Load(文件)
让元素添加=
doc.Element(xname“root”)。添加(
[xelem“位置”
[(xelem“纬度”(xstr纬度))
(xelem“经度”(xstr经度))
]
])
docAmended.Save(文件)
具有
| :? IsolatedStorageException为ex->MessageBox.Show(“保存文件时出错:”+ex.Message)|>忽略
|->MessageBox.Show(“无法打开文件”)->忽略
而位置更改手持设备是:

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
让我的位置更改(e:GeoPositionChangedEventArgs,map:map,ellipse:ellipse)=
设ppLoc=新地理坐标(e.Position.Location.lation,e.Position.Location.Longitude)
地图设置视图(ppLoc,10.0)
//do layer.AddChild(椭圆,ppLoc)

ellipse.Visibility正如我在评论中提到的,我不是一个F#er,但是我已经在C#中看到过几次这个错误。“使用”块很好地解决了这个问题。尽管您使用的是“use”块,但文档说最好使用“use”块来处理类似的事情,因为它听起来像“locations.xml”仍然被某些东西打开

运行以下代码时,请尝试使用“using”而不是“use”:

            ...
            ]                    
            use store = IsolatedStorageFile.GetUserStoreForApplication()                   
            if not (store.FileExists("locations.xml")) then
            ...

以下是关于原因的重要亮点(从上面的链接):


using函数和use绑定几乎是完成相同任务的等效方法。using关键字提供了对何时调用Dispose的更多控制。使用using时,在函数或lambda表达式末尾调用Dispose;使用use关键字时,在包含代码块的末尾调用Dispose。一般来说,您应该更喜欢使用use而不是use函数。

正如我在评论中提到的,我不是一个F#er,但是我已经在C#中看到过几次这个错误。“使用”块很好地解决了这个问题。尽管您使用的是“use”块,但文档说最好使用“use”块来处理类似的事情,因为它听起来像“locations.xml”仍然被某些东西打开

运行以下代码时,请尝试使用“using”而不是“use”:

            ...
            ]                    
            use store = IsolatedStorageFile.GetUserStoreForApplication()                   
            if not (store.FileExists("locations.xml")) then
            ...

以下是关于原因的重要亮点(从上面的链接):


using函数和use绑定几乎是完成相同任务的等效方法。using关键字提供了对何时调用Dispose的更多控制。使用using时,在函数或lambda表达式末尾调用Dispose;使用use关键字时,在包含代码块的末尾调用Dispose。一般来说,您应该更喜欢使用use而不是use函数。

我找到了它,并认为我会为其他有相同问题的人提供解决方案。 我需要从存储中加载XML,删除文件并关闭“使用”块,修改XML,以FileMode再次打开文件存储。创建并将修改后的XML保存回存储。这是解决方案,感谢所有帮助我解决问题的人!:

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                                      
                use store = IsolatedStorageFile.GetUserStoreForApplication()                   
                if not (store.FileExists("locations.xml")) then
                     use file = new IsolatedStorageFileStream("locations.xml", IO.FileMode.OpenOrCreate, store) 
                     let doc =
                        xdoc
                            [xelem "root"
                                [xelem "location"
                                    [(xelem "latitude" (xstr latitude))
                                     (xelem "longitude" (xstr longitude))
                                    ]
                                ]
                            ]                   
                     doc.Save(file)

                else
                    use store = IsolatedStorageFile.GetUserStoreForApplication()
                    use file = new IsolatedStorageFileStream("locations.xml", IO.FileMode.Open, store)
                    let docAmended : XDocument = XDocument.Load(file) 
                    file.Close()
                    store.DeleteFile("locations.xml")
                    use file = new IsolatedStorageFileStream("locations.xml", IO.FileMode.Create, store)
                    do
                        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
让xname n=xname.op_隐式(n)
设xdoc(el:seq)=新XDocument(Array.map框(Array.ofSeq el))
设xelem s el=新的XElement(xname s,框el)
设xstr s=box s
成员this.createLocationsFile纬度经度=
尝试
使用store=IsolatedStorageFile.GetUserStoreForApplication()命令
如果没有(store.FileExists(“locations.xml”)),则
使用file=new-IsolatedStorageFileStream(“locations.xml”,IO.FileMode.OpenOrCreate,store)
让医生=
xdoc
[xelem“根”
[xelem“位置”
[(xelem“纬度”(xstr纬度))
(xelem“经度”(xstr经度))
]
]
]                   
文件保存(文件)
其他的
使用store=IsolatedStorageFile.GetUserStoreForApplication()命令
使用file=new-IsolatedStorageFileStream(“locations.xml”,IO.FileMode.Open,store)
让文档修改:XDocument=XDocument.Load(文件)
file.Close()文件