1

我在将 ObservableCollection 保存和恢复到 IsolatedData 时遇到了很大的问题。我正在尝试使用此代码。

Observable 的辅助类

public class ListItem {
    public String Title { get; set; }
    public bool Checked { get; set; }

    public ListItem(String title, bool isChecked=false) {
        Title = title;
        Checked = isChecked;
    }
    private ListItem() { }

}

等帮手

public class IsoStoreHelper {
    private static IsolatedStorageFile _isoStore;
    public static IsolatedStorageFile IsoStore {
        get { return _isoStore ?? (_isoStore = IsolatedStorageFile.GetUserStoreForApplication()); }
    }

    public static void SaveList<T>(string folderName, string dataName, ObservableCollection<T> dataList) where T : class {
        if (!IsoStore.DirectoryExists(folderName)) {
            IsoStore.CreateDirectory(folderName);
        }

        if (IsoStore.FileExists(folderName + "\\" + dataName+".dat")) {
            IsoStore.DeleteFile(folderName + "\\" + dataName + ".dat");
        }

        string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName);
        try {
            using (var stream = new IsolatedStorageFileStream(fileStreamName, FileMode.Create, IsoStore)) {
                var dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
                dcs.WriteObject(stream, dataList);
            }
        } catch (Exception e) {
            Debug.WriteLine(e.Message);
        }
    }

    public static ObservableCollection<T> LoadList<T>(string folderName, string dataName) where T : class {
        var retval = new ObservableCollection<T>();

        if (!IsoStore.DirectoryExists(folderName) || !IsoStore.FileExists(folderName + "\\" + dataName + ".dat")) {
            return retval;
        }

        string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName);

        var isf = IsoStore;
        try {
            var fileStream = IsoStore.OpenFile(fileStreamName, FileMode.OpenOrCreate);
            if (fileStream.Length > 0) {
                var dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
                retval = dcs.ReadObject(fileStream) as ObservableCollection<T>;
            }
        } catch {
            retval = new ObservableCollection<T>();
        }

        return retval;
    }
}

我正在尝试以这种方式使用它

public partial class MainPage : PhoneApplicationPage{
    public ObservableCollection<ListItem> ListItems = new ObservableCollection<ListItem>();
    bool isListSaved;
    private void Panorama_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
        if (strTag.Equals("list") ) {
                isListSave = false;
                ListItems = IsoStoreHelper.LoadList<ListItem>("settings", "ListItems");
        } else if (!isListSave) {

                IsoStoreHelper.SaveList<ListItem>("settings", "ListItems", ListItems);
        }
    }
}

A first chance exception of type 'System.Security.SecurityException' occurred in System.Runtime.Serialization.ni.dll当我尝试在线读取保存的文件时,我不断得到,ReadObject(fileStream)但 FileAccess 看起来不错。

任何结论将不胜感激。


解决了:

就像 Dmytro Tsiniavskyi 所说,我完全忘记了 ListItem 中的 [DataContract] 和 [DataMember]。更重要的是,我找到了保存和加载数据的更好解决方案。我最终得到了 ListItem 的这段代码

[DataContract]
public class ListItem {
    [DataMember]
    public String Title { get; set; }
    [DataMember]
    public bool Checked { get; set; }

    public ListItem(String title, bool isChecked=false) {
        Title = title;
        Checked = isChecked;
    }
    private ListItem() { }

}

这个保存/加载集合的代码最初是在这里创建的,为了更好的使用做了一点修改。

public partial class IsolatedRW {
    public static void SaveData<T>(string fileName, T dataToSave) {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) {
            try {

                if (store.FileExists(fileName)) {
                    store.DeleteFile(fileName);
                }

                if (!store.DirectoryExists("Settings")) store.CreateDirectory("Settings");
                IsolatedStorageFileStream stream;
                using (stream = store.OpenFile("Settings/"+fileName+".xml", System.IO.FileMode.Create, System.IO.FileAccess.Write)) {
                    var serializer = new DataContractSerializer(typeof(T));
                    serializer.WriteObject(stream, dataToSave);
                }
                stream.Close();
            } catch (System.Security.SecurityException e) {
                //MessageBox.Show(e.Message);
                return;
            }
            Debug.WriteLine(store.FileExists("Settings/" + fileName + ".xml"));
        }
    }

    public static T ReadData<T>(string fileName) {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) {

            Debug.WriteLine(store.FileExists("Settings/" + fileName + ".xml"));
            if (store.FileExists("Settings/" + fileName + ".xml")) {
                IsolatedStorageFileStream stream;
                using (stream = store.OpenFile("Settings/"+fileName+".xml", FileMode.OpenOrCreate, FileAccess.Read)) {

                    try {
                        var serializer = new DataContractSerializer(typeof(T));
                        return (T)serializer.ReadObject(stream);

                    } catch (Exception) {
                        return default(T);
                    }
                }
                stream.Close();
            }
            return default(T);
        }
    }
}
4

1 回答 1

1

尝试为您的班级添加[DataContract]属性。ListItem

[DataContract]
public class ListItem {

    [DataMember]
    public String Title { get; set; }

    [DataMember]
    public bool Checked { get; set; }

    public ListItem(String title, bool isChecked=false) {
        Title = title;
        Checked = isChecked;
    }

    private ListItem() { }
}
于 2013-11-03T14:53:38.000 回答