While porting wordastic from Windows Phone to Windows 8, I had to port a few classes. Persistance helper was one of them and is used to read / write objects to storage.
In Windows Phone dev, you can consume Silverlight IsolatedStorageFile for storing files and settings. With WinRT, IsolatedStorageFile isn’t available.
A few accessible objects are StorageFile and StorageFolder. Using these, we can get handle to file stream and consume it like we would in .NET. You get access to raw stream and you can consume it in whatever manner you feel comfortable. I prefer StreamReader / StreamWriter. Here’s the code. if you need any further info let me know
public class PersistHelper
{
private async static Task<StorageFile> GetFileIfExistsAsync(StorageFolder folder, string fileName)
{
try
{
return await folder.GetFileAsync(fileName);
}
catch
{
return null;
}
}
public static async Task<T> LoadObjectFromStorage<T>()
{
T ObjToLoad = default(T);
try
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
string filename = GetFileName<T>();
StorageFile storageFile = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
//using (Stream inStream = await storageFolder.OpenStreamForReadAsync(filename))
using (Stream inStream = await storageFile.OpenStreamForReadAsync())
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
ObjToLoad = (T)serializer.Deserialize(inStream);
}
}
catch (Exception error)
{
throw new NotImplementedException(error.Message);
}
return ObjToLoad;
}
public static async void SaveObjectToStorage<T>(T ObjectToSave)
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
string filename = GetFileName<T>();
using (Stream fs = await storageFolder.OpenStreamForWriteAsync(filename, CreationCollisionOption.ReplaceExisting))
{
using (StreamWriter sw = new StreamWriter(fs))
{
XmlSerializer ser = new XmlSerializer(typeof(T));
ser.Serialize(sw, ObjectToSave);
}
}
}
public static string GetFileName<T>()
{
return typeof(T).FullName + ".xml";
}
public async static Task<bool> IsObjectPersisted<T1>()
{
string file = GetFileName<T1>();
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile storageFile = await GetFileIfExistsAsync(storageFolder, file);
return (storageFile != null);
}
public static T LoadSetttingFromStorage<T>(string Key)
{
T ObjToLoad = default(T);
if (ApplicationData.Current.LocalSettings.Values.ContainsKey(Key))
{
using (StringReader sr = new StringReader((string)ApplicationData.Current.LocalSettings.Values[Key]))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
ObjToLoad = (T)serializer.Deserialize(sr);
}
}
return ObjToLoad;
}
public static void SaveSettingToStorage(string Key, object Setting)
{
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
XmlSerializer ser = new XmlSerializer(Setting.GetType());
ser.Serialize(sw, Setting);
}
if (!ApplicationData.Current.LocalSettings.Values.ContainsKey(Key))
{
ApplicationData.Current.LocalSettings.Values.Add(Key, sb.ToString());
}
else
{
ApplicationData.Current.LocalSettings.Values[Key] = sb.ToString();
}
}
public static bool IsSettingPersisted(string Key)
{
return ApplicationData.Current.LocalSettings.Values.ContainsKey(Key);
}
}