【问题标题】:IsolatedStorageException Operation Not Permitted when calling CreateFile Repeatedly重复调用 CreateFile 时,IsolatedStorageException 操作不允许
【发布时间】:2013-05-16 01:12:58
【问题描述】:

我已经阅读了大约一千篇类似的帖子,并遵循了一般建议,但仍然遇到问题。这是我的场景:

我正在开发一个 Windows Phone 8 应用程序,当用户保存时,它会将所有数据序列化为 XML,然后使用 CreateFile 来存储它。我面临的问题是,如果用户连续多次点击保存,则会引发 IsolatedStorageException:Operation Not Permitted(我猜序列化需要足够长的时间,以至于当我尝试访问该文件时仍在使用它第二次)。当第二次点击保存时,有没有办法让我中止之前的操作,释放隔离的存储文件并启动新的保存?还是有更好的解决方案?

这是我的 Save 方法的代码(异常发生在 isoStore.CreateFile(filename) 行):

        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {

            using (IsolatedStorageFileStream stream = isoStore.CreateFile(filename))
            {
                XmlSerializer xml = new XmlSerializer(GetType());
                xml.Serialize(stream, this);
            }
        }

任何帮助都会很棒,因为我已经被困在这里好几个星期了。

谢谢, 本:

【问题讨论】:

    标签: c# windows-phone-8 isolatedstorage


    【解决方案1】:

    你可以用这样的东西。

    private async Task Save(string fileName)
    {
        Button.IsEnabled = false;
    
        await Task.Run(() =>
            {
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
    
                    using (IsolatedStorageFileStream stream = isoStore.CreateFile(filename))
                    {
                        XmlSerializer xml = new XmlSerializer(GetType());
                        xml.Serialize(stream, this);
                    }
                }
            });
    
        Button.IsEnabled = true;
    }
    

    【讨论】:

      【解决方案2】:

      为什么不禁用单击时的“保存”按钮,然后在序列化完成后再次启用它?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-08
        • 2015-12-29
        • 1970-01-01
        • 1970-01-01
        • 2013-03-23
        • 2017-05-14
        • 2011-11-23
        • 1970-01-01
        相关资源
        最近更新 更多