设为首页 收藏本站
查看: 745|回复: 0

漫谈windows phone 7 独立存储(一)

[复制链接]

尚未签到

发表于 2015-5-8 14:30:11 | 显示全部楼层 |阅读模式
  独立存储(Isolated Storage),顾名思义,也就是在windows phone 7手机上存储本地数据。独立存储并不是wp7特有的,早先是出现在silverlight中。且所有的I/O操作是局限于独立存储,不能直接访问OS文件系统,但是独立存储会提供API来操作文件系统。
  wp7提供了两种方式在本地存储数据:
  •     IsolatedStorageFile(独立文件存储): 命名空间为:System.IO.IsolatedStorage.IsolatedStorageFile,可以在虚拟的独立存储中创建、使用、删除文件和目录, 使用System.IO.IsolatedStorage.IsolatedFileStream,通过文件流(file stream)可以添加或者检索文件。独立文件流可以存储从web动态加载的图片、声音和文件。
  •     IsolatedStorageSettings(独立本地设置): 命名空间为:System.IO.IsolatedStorage.IsolatedStorageSettings,提供了一系列的API,用来在独立存储中存储和操作键/值对。一般使用该方式来存储App设置和用户特定设置。
DSC0000.jpg
  (来自MSDN)
  
  (1)使用IsolatedStorageFile
  

  效果图:
DSC0001.jpg DSC0002.jpg
  
  
  在本例中,当我们点击“Save to IsolatedStorage”时,会通过IsolatedStorageFileStream将图片存储到独立存储中,当点击“Read from IsolatedStorage”时,会从独立存储中加载出图片文件,并在image控件上展示。当点击“Save to Media library”时,我们会将图片存入windows phone上的Media Library中。
  保存图片到独立存储:
  首先我们创建一个虚拟的存储器,检查在独立存储中是否已经存在要保存的文件,此后,就可以保存文件到独立存储。
  
               // Create a filename for JPEG file in isolated storage.
            String tempJPEG = "logo.jpg";

            // Create virtual store and file stream. Check for duplicate tempJPEG files.
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(tempJPEG))
                {
                    myIsolatedStorage.DeleteFile(tempJPEG);
                }

                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

                StreamResourceInfo sri = null;
                Uri uri = new Uri(tempJPEG, UriKind.Relative);
                sri = Application.GetResourceStream(uri);

                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wb = new WriteableBitmap(bitmap);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

                //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                fileStream.Close();
            }
  我们也可以使用wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);来保存图片。
  
  从独立存储中读取图片:
  
  首先我们从独立存储中打开该图片文件,然后再读取它的内容。代码如下:



            BitmapImage bi = new BitmapImage();

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
                {
                    bi.SetSource(fileStream);
                    this.img.Height = bi.PixelHeight;
                    this.img.Width = bi.PixelWidth;
                }
            }
            this.img.Source = bi;
  存储图片到媒体库:
  注意:需要添加引用(Microsoft.XNA.Framework)。
  我们创建一个新的IsolatedStorageFileStream,用来从独立存储中读取文件,之后,存储文件到媒体库。代码如下:



            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
                {
                    MediaLibrary mediaLibrary = new MediaLibrary();
                    Picture pic = mediaLibrary.SavePicture("SavedLogo.jpg", fileStream);
                    fileStream.Close();
                }
            }

            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Show();
  View:

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65040-1-1.html 上篇帖子: Windows Phone 7 自定义事件 下篇帖子: 微软公布Windows Phone 7 开发路线图
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表