2012-04-16 88 views
2

我有一个跟踪用户位置的WP7应用程序。所有工作都很好,除了我想将位置写入GeoCoordinate监视器的位置更改事件上的隔离存储器,并且我一直收到“IsolatedStorageFileStream中不允许的操作”消息。任何人都可以帮助获得这个工作?F#windows手机隔离存储和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 
       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 
+0

错误听起来像是东西在孤立的存储保存文件。查看您是否可以在createLocationsFile事件的if和else情况下处理该文件的句柄。不幸的是,我不是F#呃,对不起,但这就是我要在C#代码中检查的内容。 – Eugene 2012-04-16 09:20:31

+0

你没有处理'IsolatedStorageFileStream'实例。尝试用'use file = ...'替换'let file = ...'。 – pad 2012-04-16 10:50:26

回答

0

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

尝试 “使用” 而不是 “用” 当您运行下面的代码字:

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

Source

这里是重要的亮点,为什么(从上面的链接):

的使用功能和使用结合几乎等同的方式来完成同样的事情。使用关键字可以更好地控制何时调用Dispose。当你使用using时,在函数或lambda表达式的末尾调用Dispose;当您使用use关键字时,在包含代码块的末尾调用Dispose。一般来说,您应该更喜欢使用use而不是using函数。

+0

以及我尝试了'使用'而不是'let'的建议,现在我已经创建了该文件,但遇到应用程序无法打开文件的错误。我已经检查过IsolatedStorage,并且确定location.xml存在于根目录中,但无法打开!也许我在代码的“其他”部分缺少一些东西? – Sonya 2012-04-16 10:56:00

+0

我的建议是改变'使用'到'使用',我没有说'让',垫说:) :) – Eugene 2012-04-16 11:24:53

+0

似乎文件正在写入IsolatedStorage,但试图在创建后更新文件,文档声明再次被添加,抛出一个XML异常。任何任何想法?我试过在更新前删除文件,但我得到一个IsolatedStorage异常,无法访问商店:( – Sonya 2012-04-16 12:43:34

0

我明白了,并认为我会将解决方案提供给有相同问题的任何人。 我需要从商店加载XML,删除文件并关闭“使用”块,修改XML,再次在FileMode.Create中打开文件存储并将修改后的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