Serialization
Objects stored in the IsolatedStorageSettings. ApplicationSettings dictionary must be serialized. All these objects are serialized and stored in an XML file named _ ApplicationSettings. If an object fails to be serialized into the dictionary, it does not report an error, but fails without sound.
All basic types, basic types, and classes composed of basic types can be serialized successfully. Therefore, it can be considered that nothing cannot be serialized, if you want to make a field not serialized, you can add the IgnoreDataMember attribute to exclude this field.
Note: although you can serialize multiple references pointing to the same object, after deserialization, it does not point to the same object, but multiple copies. Properly handle this issue.
Load images from files/cameras
if (age.PhotoFilename != null){ this.BackgroundImage.Source = IsolatedStorageHelper.LoadFile(age.PhotoFilename);}
The following function provides multiple functions for obtaining, saving, deleting, and decoding image files:
void PictureButton_Click(object sender, EventArgs e){ Microsoft.Phone.Tasks.PhotoChooserTask task = new PhotoChooserTask(); task.ShowCamera = true; task.Completed += delegate(object s, PhotoResult args) { if (args.TaskResult == TaskResult.OK) { string filename = Guid.NewGuid().ToString(); IsolatedStorageHelper.SaveFile(filename, args.ChosenPhoto); Age age = Settings.List.Value[Settings.CurrentAgeIndex.Value]; if (age.PhotoFilename != null) IsolatedStorageHelper.DeleteFile(age.PhotoFilename); age.PhotoFilename = filename; // Seek back to the beginning of the stream args.ChosenPhoto.Seek(0, SeekOrigin.Begin); // Set the background image instantly from the stream // Turn the stream into an ImageSource this.BackgroundImage.Source = PictureDecoder.DecodeJpeg( args.ChosenPhoto, (int)this.ActualWidth, (int)this.ActualHeight); } }; task.Show();}
Image File Operations with Cache
public static class IsolatedStorageHelper{ static Dictionary<string, ImageSource> cache = new Dictionary<string, ImageSource>(); public static void SaveFile(string filename, Stream data) { using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFileStream stream = userStore.CreateFile(filename)) { // Get the bytes from the input stream byte[] bytes = new byte[data.Length]; data.Read(bytes, 0, bytes.Length); // Write the bytes to the new stream stream.Write(bytes, 0, bytes.Length); } } public static ImageSource LoadFile(string filename) { if (cache.ContainsKey(filename)) { return cache[filename]; } using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFileStream stream = userStore.OpenFile(filename, FileMode.Open)) { // Turn the stream into an ImageSource ImageSource source = PictureDecoder.DecodeJpeg(stream); cache[filename] = source; return source; } } public static void DeleteFile(string filename) { using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication()) userStore.DeleteFile(filename); }}
The execution speed of PictureDecoder. DecodeJpeg method is slow and must be called by the UI thread, which affects the user experience. Therefore, cache is used here.
We recommend that you use the WriteableBitmap. LoadJpeg method if you have a large number of images (cache is inconvenient) and you need to attach images frequently, because this method can be called by background threads.
This class is a common file operation helper class.