Day one went less than stellar. Thought I really had a great, easy, first day idea. I created a little app called Black Book, a simple app that allows the user to keep contacts in a separate location.
Everything went well at first, created the data model, got the code done to save the data, got the sorting code so that the names would come up in the right order, bound my collection to the list on the main page.
The code I want to focus on from this first app is the Isolated Storage code that I used to save my collection of Contact objects.
Now, when using Isolated Storage there are two main paths that
developers usually choose from,
- Storing single, usually simple objects to a key within the application.
- Storing serialized data, usually a more complex object or collection.
The first choice is most commonly used for saving page state or what is more commonly known as TombStoning.
Since I am storing a collection, I chose the slightly more complicated second choice.
Saving a collection in Isolated Storage
- You need to start by adding a reference to
System.Runtime.Serialization
- You are best off to create a class like:
[DataContract] public class Data { }
- You will need to add the following references to your class:
using System.Collections.ObjectModel; using System.IO; using System.IO.IsolatedStorage; using System.Runtime.Serialization; using System.Windows;
- Now, to save your collection, you will add the following:
/// <summary> /// Saves a submitted Observable Collection of Contacts to IsolatedStorage /// </summary> /// <param name="contacts">Observable Collection of Contacts to be saved</param> public static void SaveOfflineData(ObservableCollection<Contact> contacts) { try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { // Create the directory in the root. store.CreateDirectory("<YOUR_APP_NAME>"); // Create the subdirectory under your app's main folder string offlineData = Path.Combine("<YOUR_APP_NAME>", "<YOUR_FOLDER_NAME>"); if (!store.DirectoryExists(offlineData)) store.CreateDirectory(offlineData); //string offlineDataFile = Path.Combine(offlineData, "SavedBills.xml"); string offlineDataFile = Path.Combine(offlineData, "<YOUR_FILE_NAME>"); if (store.FileExists(offlineDataFile)) { store.DeleteFile(offlineDataFile); } IsolatedStorageFileStream dataFile = dataFile =
store.OpenFile(offlineDataFile, FileMode.OpenOrCreate); DataContractSerializer ser =
new DataContractSerializer(typeof(ObservableCollection<Contact>)); ser.WriteObject(dataFile, contacts); dataFile.Close(); } } catch (IsolatedStorageException) { // Fail gracefully } }
Retrieving a collection from Isolated Storage
Retrieving it is simple, to your existing class, simply add the following method
/// <summary> /// Retreives the collection of Contacts from the IsolatedStorage /// </summary> /// <returns>Observable Collection of Contacts</returns> public static ObservableCollection<Contact> GetSavedData() { ObservableCollection<Contact> contacts = new ObservableCollection<Contact>(); try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { string offlineData = Path.Combine("<YOUR_APP_NAME>", "<YOUR_FOLDER_NAME>"); string offlineDataFile = Path.Combine(offlineData, "<YOUR_XML_FILE_NAME>"); IsolatedStorageFileStream dataFile = null; if (store.FileExists(offlineDataFile)) { dataFile = store.OpenFile(offlineDataFile, FileMode.Open); DataContractSerializer ser =
new DataContractSerializer(typeof(ObservableCollection<Contact>)); // Deserialize the data and read it contacts = (ObservableCollection<Contact>)ser.ReadObject(dataFile); fileSize = dataFile.Length; dataFile.Close(); } else { //MessageBox.Show("No data available"); MessageBox.Show(Resource.dataResource.errorMessageNoDataAvailable); } } } catch (IsolatedStorageException) { // Fail gracefully } return contacts; }
So, you can see that using Isolated storage is really pretty easy.
The only problem I ended up having with building this app is when I tried to add a star rating on a couple of the forms. I really wanted to have a star rating on the form and I had seen it on other apps, but apparently had no idea how to do it.
I got a couple of examples and wasted way too much time trying to get any of them to work in my app.
Anyways, I am tired, I am two hours late on my first day and had to remove several features including:
- Password protection (I have all the code done except the login popup)
- Star rating control.
I will have to add these two things in an update. The app is up awaiting certification and I will start on another one tomorrow.
Atley, what database do you use for storing data? Do you use Isolated Storage or…