A damn at han’s Windows phone book 笔记(23:序列化,图片)
序列化
存储在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 streamargs.ChosenPhoto.Seek(0, SeekOrigin.Begin);// Set the background image instantly from the stream// Turn the stream into an ImageSourcethis.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 streambyte[] bytes = new byte[data.Length];data.Read(bytes, 0, bytes.Length);// Write the bytes to the new streamstream.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 ImageSourceImageSource 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方法,因为该方法可以由后台线程调用。
这个类修改一下就是通用的文件操作辅助类。
转载于:https://www.cnblogs.com/dc10101/archive/2011/10/23/2221784.html
A damn at han’s Windows phone book 笔记(23:序列化,图片)相关推荐
- A damn at han’s Windows phone book 笔记(2:Flashlight)
添加Application Bar 用 Expression Blend很直观. 添加Application Bar:右击 Objects and Timeline 面板上的PhoneApplicat ...
- A damn at han’s Windows phone book 笔记(3:ICE——In Case of Emergency)
SupportedOrientations 和 Orientation SupportedOrientations比较有用,而Orientation只能影响到设计时的状态,无法影响运行时的状态,因此可 ...
- Windows下C 用 Socket 发送图片--基础
Windows下C 用 Socket 发送图片--基础 转载:http://blog.csdn.net/yulinxx/article/details/51338214 服务器端: #include ...
- Windows异常学习笔记(五)—— 未处理异常
Windows异常学习笔记(五)-- 未处理异常 要点回顾 最后一道防线 实验一:理解最后一道防线 实验二:新线程的最后一道防线 总结 UnhandledExceptionFilter 实验三:理解U ...
- Windows异常学习笔记(四)—— 编译器扩展SEH
Windows异常学习笔记(四)-- 编译器扩展SEH 要点回顾 编译器支持的SEH 过滤表达式 实验一:理解_try_except 实验二:_try_except 嵌套 拓展SEH结构体 scope ...
- Windows异常学习笔记(二)—— 内核异常处理流程用户异常的分发
Windows异常学习笔记(二)-- 内核异常处理流程&用户异常分发 用户层与内核层异常 内核异常 分析 KiDispatchException 分析 RtlDispatchException ...
- Windows异常学习笔记(一)—— CPU异常记录模拟异常记录
Windows异常学习笔记(一)-- CPU异常记录 基础知识 异常的分类 CPU异常 分析中断处理函数 _KiTrap00 分析 CommonDispatchException 总结 软件模拟异常 ...
- Windows APC学习笔记(二)—— 挂入过程执行过程
Windows APC学习笔记(二)-- 挂入过程&执行过程 基础知识 挂入过程 KeInitializeApc ApcStateIndex KiInsertQueueApc Alertabl ...
- Windows APC学习笔记(一)—— APC的本质备用APC队列
Windows APC学习笔记(一)-- APC的本质&备用APC队列 基础知识 APC的本质 APC队列 APC结构 分析 KiServiceExit 总结 备用APC队列 挂靠环境下Apc ...
最新文章
- Android的进程优先级
- 贴片铝电容识别及型号_贴片钽电容封装及规格和参数资料
- 拖动窗体的任意区域移动窗体
- javaScript获取url中的参数
- 使用Gradle的maven-publish插件发布快照
- 字符串匹配之KMP(KnuthMorrisPratt)算法(图解)
- Python GUI界面编程初步 05- GUI框架PyQt的运用 - 01 PyQt的详细安装和基本使用
- python从网址爬图片协程_python 用 gevent 协程抓取海量网页
- Hadoop平台 以Parcel包安装CDH
- OSChina 周日乱弹 —— 普通人如何面对持刀歹徒
- FLStudio21无需切换中文语言fl下载中文免费版
- mysql b 树 字符串索引_Mysql从入门到入神之(四)B+树索引
- iMeta | 海南大学张家超组-基于肠道菌群SNV基因标记物的炎症性肠病预测模型
- Labview 编写TCP/IP 客户端断线重连机制程序,亲测可用
- paper survey(2019.06.05)——卷积网络feature map的传递与利用
- 为什么有人说C++是最难学的编程语言? (4个回答)
- css字行高怎么设,css文本行高怎么设置-电脑自学网
- 剥开ios 系统sandbox神秘面纱
- 【论文精读】Temporal Fusion Transformers for Interpretable Multi-horizon Time Series Forecasting
- 2021-11-14阿迪看医生
热门文章
- docker创建image
- pythonchallenge--0
- 注册表只改一个值 马上加快宽带上网速度
- 进程间通信(IPC)之内存映射mmap和共享内存shm
- 360算法技术解密与实践-技术干货满满哒
- SpringCloud Sleuth + zipkin 实现微服务链路追踪功能
- web mysql数据库的持久连接_JavaWeb连接数据库MySQL的操作技巧
- python print(len(pi_string))_Python如何从文件中读取数据
- linux下执行mysql的sql文件
- android 脚本录制工具,[atx 系列] android 脚本录制