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

独立存储(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

 

效果图:

GGXCXKC9FQNFKW4BP@NDL%U{T$MFA1$8)Q`{4)6%OTT893

 

 

在本例中,当我们点击“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:

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
 
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
 
        <!--ContentPanel - place additional content here-->
        <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Save to IsolatedStorage" x:Name="btnSave" Click="btnSave_Click"/>
            <Button Content="Read from IsolatedStorage" x:Name="btnRead" Click="btnRead_Click"/>
            <Image x:Name="img"/>
            <Button Content="Save to Media library" x:Name="btnMedia" Click="btnMedia_Click"/>
        </StackPanel>
    </Grid>
posted @ 2011-09-29 10:35  sunfish  阅读(1371)  评论(2编辑  收藏  举报