独立存储(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设置和用户特定设置。
(来自MSDN)
(1)使用IsolatedStorageFile
效果图:
在本例中,当我们点击“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