序列化

存储在IsolatedStorageSettings.ApplicationSettings字典中的对象都要能被序列化。所有这些对象都被序列化存放在一个叫做__ApplicationSettings的XML文件中。如果有对象无法成功地序列化进字典,它也不会报错,而是不声不响地失败。

所有的基础类型,基础类型的集合,以及由基础类型构成的类都可以成功序列化,因此可以认为,没有什么是不能序列化的,如果要有意让某个字段不进行序列化,则可以加上IgnoreDataMember属性来排除该字段。

注意:尽管你可以将指向同一个对象的多个引用序列化,但是反序列化之后,就不是指向同一个对象了,而是多份拷贝。要妥善处理好这个问题。

 
从文件/相机加载图片
if (age.PhotoFilename != null)
{
    this.BackgroundImage.Source = IsolatedStorageHelper.LoadFile(age.PhotoFilename);
}
下面这个函数包含了图片文件的获取、保存、删除、解码等多个功能:
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();
}

带缓存的图片文件操作辅助类

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);
    }
}

PictureDecoder.DecodeJpeg方法执行速度慢,而且必须要UI线程调用,因此会影响用户体验,所以这里用到了cache。

如果有特别多的图片(不便使用cache)且经常要加载图片,建议用WriteableBitmap.LoadJpeg方法,因为该方法可以由后台线程调用。

这个类修改一下就是通用的文件操作辅助类。

posted on 2011-10-23 15:06  MainTao  阅读(463)  评论(0编辑  收藏  举报