A damn at han's Windows phone book notes (23: serialization, images)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.