我有很大的问题,saveing和恢复的ObservableCollection到IsolatedData。 我想这个code。
辅助类的观测
公共类列表项{
公共字符串名称{获取;组; }
公共BOOL经过{获得;组; }
公共列表项(字符串标题,布尔=器isChecked FALSE){
标题=称号;
选中=器isChecked;
}
私人列表项(){}
}
IsoHelper
公共类IsoStoreHelper {
私有静态IsolatedStorageFile _isoStore;
公共静态IsolatedStorageFile IsoStore {
{返回_isoStore? (_isoStore = IsolatedStorageFile.GetUserStoreForApplication()); }
}
公共静态无效SaveList< T>(串夹folderName,字符串数据名,的ObservableCollection< T> DataList控件)其中T:类{
如果(!IsoStore.DirectoryExists(夹folderName)){
IsoStore.CreateDirectory(夹folderName);
}
如果(IsoStore.FileExists(夹folderName +\\+数据名+。DAT)){
IsoStore.DeleteFile(夹folderName +\\+数据名+.DAT);
}
字符串fileStreamName =的String.Format({0} \\ {1} .DAT,夹folderName,数据名);
尝试 {
使用(VAR流=新IsolatedStorageFileStream(fileStreamName,FileMode.Create,IsoStore)){
VAR DCS =新的DataContractSerializer(typeof运算(的ObservableCollection< T>));
dcs.WriteObject(流,DataList控件);
}
}赶上(例外五){
的Debug.WriteLine(e.Message);
}
}
公共静态的ObservableCollection< T> LoadList< T>(串夹folderName,字符串数据名),其中T:类{
VAR RETVAL =新的ObservableCollection< T>();
如果(!IsoStore.DirectoryExists(夹folderName)||!IsoStore.FileExists(夹folderName +\\+数据名+.DAT)){
返回RETVAL;
}
字符串fileStreamName =的String.Format({0} \\ {1} .DAT,夹folderName,数据名);
VAR ISF = IsoStore;
尝试 {
VAR FILESTREAM = IsoStore.OpenFile(fileStreamName,FileMode.OpenOrCreate);
如果(fileStream.Length大于0){
VAR DCS =新的DataContractSerializer(typeof运算(的ObservableCollection< T>));
RETVAL = dcs.ReadObject(FILESTREAM)为的ObservableCollection< T&GT ;;
}
} 抓住 {
RETVAL =新的ObservableCollection< T>();
}
返回RETVAL;
}
}
和我想用这种方式
公共部分MainPage的类:PhoneApplicationPage {
公众的ObservableCollection<列表项> ListItems的=新的ObservableCollection<列表项>();
布尔isListSaved;
私人无效Panorama_SelectionChanged(对象发件人,System.Windows.Controls.SelectionChangedEventArgs E){
如果(strTag.Equals(名单)){
isListSave = FALSE;
ListItems的= IsoStoreHelper.LoadList<列表项>(设置,ListItems的);
}否则,如果(!isListSave){
IsoStoreHelper.SaveList<列表项>(设置,ListItems的,ListItems的);
}
}
}
我不断收到 A第一次机会异常的类型'System.Security.SecurityException发生在System.Runtime.Serialization.ni.dll
当我尝试读取保存的文件在行的readObject(FILESTREAM)
不过的FileAccess看起来不错。
任何结论,将AP preciated。
就像德米特罗Tsiniavskyi说我完全以忘了[DataContract]和[DataMember]标注在列表项。什么是更多,我发现了保存和读取数据更好的解决方案。我结束了这个$ C $下列表项
[DataContract]
公共类列表项{
[数据成员]
公共字符串名称{获取;组; }
[数据成员]
公共BOOL经过{获得;组; }
公共列表项(字符串标题,布尔=器isChecked FALSE){
标题=称号;
选中=器isChecked;
}
私人列表项(){}
}
这$ C $下保存/这是最初成立这里和修改豆蔻位为更好的负载集合使用率。
公共部分类IsolatedRW {
公共静态无效的保存数据< T>(字符串文件名,T dataToSave){
使用(IsolatedStorageFile店= IsolatedStorageFile.GetUserStoreForApplication()){
尝试 {
如果(store.FileExists(文件名)){
store.DeleteFile(文件名);
}
如果store.CreateDirectory(设置)(store.DirectoryExists(设置)!);
IsolatedStorageFileStream流;
使用(流= store.OpenFile(设置/+文件名+XML,System.IO.FileMode.Create,System.IO.FileAccess.Write)){
无功序列化=新的DataContractSerializer(typeof运算(T));
serializer.WriteObject(流dataToSave);
}
stream.Close();
}赶上(System.Security.SecurityException E){
//MessageBox.Show(e.Message);
返回;
}
的Debug.WriteLine(store.FileExists(设置/+文件名+的.xml));
}
}
公共静态牛逼READDATA< T>(字符串文件名){
使用(IsolatedStorageFile店= IsolatedStorageFile.GetUserStoreForApplication()){
的Debug.WriteLine(store.FileExists(设置/+文件名+的.xml));
如果(store.FileExists(设置/+文件名+的.xml)){
IsolatedStorageFileStream流;
使用(流= store.OpenFile(设置/+文件名+XML,FileMode.OpenOrCreate,FileAccess.Read)){
尝试 {
无功序列化=新的DataContractSerializer(typeof运算(T));
返程(T)serializer.ReadObject(流);
}赶上(例外){
返回默认(T);
}
}
stream.Close();
}
返回默认(T);
}
}
}
解决方案
尝试添加 [DataContract]
属性的列表项
类。
[DataContract]
公共类列表项{
[数据成员]
公共字符串名称{获取;组; }
[数据成员]
公共BOOL经过{获得;组; }
公共列表项(字符串标题,布尔=器isChecked FALSE){
标题=称号;
选中=器isChecked;
}
私人列表项(){}
}
I have huge problem with saveing and restore ObservableCollection to IsolatedData. I'm trying with this code.
Helper class for 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() { }
}
IsoHelper
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;
}
}
And I'm trying to use it this way
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);
}
}
}
I keep getting A first chance exception of type 'System.Security.SecurityException' occurred in System.Runtime.Serialization.ni.dll
when I try read saved file at line ReadObject(fileStream)
but the FileAccess looks fine.
Any conclusion will be appreciated.
Like Dmytro Tsiniavskyi said I totaly forgot about [DataContract] and [DataMember] in ListItem. Whats more I found better solution for saving and loading data. I end up with this code for 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() { }
}
And this code for save/load collection which was originally founded here and modified a litte bit for better useage.
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);
}
}
}
解决方案
Try to add [DataContract]
attribute for your ListItem
class.
[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() { }
}