序列化
存储在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方法,因为该方法可以由后台线程调用。
这个类修改一下就是通用的文件操作辅助类。
分类:
Windows Phone
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架