Windows Phone8开发入门(四)

来源:互联网 发布:h5小黄人接金币源码 编辑:程序博客网 时间:2024/06/04 05:55



Windows Phone 8文件和存储


 


WP7.1 IsolatedStorageIsolatedStorageSetting APIs


 


“本地存储”和“独立存储”


读取文件的不同方式:


Local Database data context


Files acess using WP7.1 Isolated Storage API


Files access using Windows.Storage API via URIs


File access using Windows.Storage API via StorageFolder references


WP8文件访问的选择:


//WP7.1 IsolatedStorage APIs


Var isf=IsolatedStorageFile.GetUserStoreForApplication();


IsolatedStorageFileStream fs=newIsolatedStorageFileStream("CaptainsLog.store",FileMode.Open,isf));


···


//WP8 Storage APIs using URI


StorageFile StorageFile=awaitWindowsd.Storage.StorageFile.GetFileFromApplicationUriAsync(newUri("ms-appdata:///local/CaptainsLog.store"));


```


//WP8 Storage APIs


Windows.Storage.StorageFolderlocalFolder=Windows.Storage.ApplicationData.Current.LocalFolder;


Windows.Storage.StorageFile storageFile=awaitlocalFolder.GetFileAsync("CaptainsLog.store");


```


使用WP7.1独立存储API保存数据


Isolated Storage Classes


IsolatedStorageFile


IsolatedFileStream


IsolatedStorageSettings


Saving Data


Private void saveGameToIsolatedStorage(string message)


{


Using(isolatedStorageFileisf=IsolatedStorageFile.GetUserStoreForApplication())


{


Using(IsolatedStorageFileSytreamrawStream=isf.CreaqteFile("MyFile.store"))


{


StreamWriter writer=new StreamWriter(rawStream);


Writer.WriteLine(message);      //save the message


Writer.Close();


}


}


 


Loading Data


Private String loadString()


{


String result=null;


Using(IsolatedStorageFileisf=IsolostedStorageFile.GetUserStoreForApplication())


{


If(isf.FileExists("Myfile.store")


{


Using(IsolatedStorageFileStreamrawStream=isf.OpenFile(filename,System.IO.FileMode.Open())


{


StreamReader  reader=newStreamReader(rawStream);


Result=reader.ReadLine();


Reader.Close();


}


}


}


Return result;


}


在应用设备中保存数据


Void saveString(streing message,string name)


{


IsolatedStorageSettings.ApplicationSettings[name]=message;


IsolatedStorageSettings.ApplicationSettings.Save();


 


 


使用设置存储


使用StorageFolder保存数据


Using System.IO;


Using Windows.Storage;


```


Private async void saveToLocalFolderAsync(string message)


{


//Get a reference to the Local Folder


StorageFolder localFolder=ApplicationData.Current.LocalFolder;


//Create the file in the local folder, or if it already exists, justopen it


StorageFile storageFile=await storageFile.OpenStreamForWreteAsync();


Using(StreamWriter writer=new StreamWriter(writeStream))


{


Await writer.WriteAsync(logData);


}


}


使用ms-appdata:///local/or ms-appx:///访问文件


//There's no FileExists method in WinRT, so have to try to open itand catch exception instead


StorageFile storageFile=null;


Bool fileExists=false;


Try


{


//Try to open file using URI


storageFile=await StorageFile.GetFileFromApplicationUriAsync(


NewUri("ms-appdata:///local/Myfile,store")


fileExists=true;


}


Catch(FileNotFoundException)


{


fileExists=false;


}


If(!fileExists)


{


AwaitApplicationData.Current.LoaclFolder.CreateFileAsync("Myfile.store",CreationCollisionOption.FaqilIfExists);


}


```


 


Windows.comStorageWindowsPhoneRunting)编程


Windows Phone8Windows8兼容性


Windows8中您可以以编程的方式加载应用程序包中的图像文件,将图像加载至XAML<Image>元素,代码如下:


RecipeImage.Source=new System.Windows.Media.Imaging.BitmapImage(newUri(@"ms-appx:///Images/french/French_1_600_C.jpg",UriKind.RelativeOrAbsolute));


WindowsPhone8中不能使用这样的URI语法,只能使用WindowsPhone OS7.1中的相对路径,代码如下:


RecipeImage.Source=newSystem.Windows.Media.Imgine.BitmapImage("/Images/french/French_1_600_C.jpg");


 


特殊文件夹


Shared/Media专辑封面


Shared/ShellContent图块


Shared/Transfers后台文件传输


数据的序列化和反序列化(将对象集合的属性写入文件以便永久保存)


->Deactivated/Closig->Not running->Launching->Running


使用DataContractSerializer实现序列化


Public class MyDataSerializer<TheDataType>


{


Public static async Task SaveObjectsAsync(TheDataTypesourceData,String targetFileName)


{


StorageFile file=await|ApplicationData.Current.LocalFolder.CreateFileAsync(


targertFileName,CreationCollisionOpetion.ReplaceExisting);


Var outStream=await file.OpenStreamForWriteAsync();


DataContractSerializer serializer=newDataContractSerizlizer(typeof(TheDataType));


Serializer.WriteObject(outStream,sourceData);


Await outStream.FlushAsync();


outStream.Close();


}


```


}


使用DataContractSerializer实现反序列化


Puboic class MyDataSerializer<TheDataType>


{


Public static async Task<TheDataType>RestoreObjectsAsync(string fileName)


{


StorageFile file=awaitApplicationData.Current.LocalFolder.GetFileAsync(fileName);


Var inStream=await file.OpenstreamForReadAsync();


//Deserialize the objects.


DataContractSerializer serializer=newDataContractSerializer(typeof(TheDataType));


TheDataType data=(TheDataType)serializer.ReadObject(inStream);


inStream.Close();


Return data;


}


````


}


 


使用独立存储资源管理器ISETool.exe


语法:


Ts-Take snapshot


Re-Restore snapshot


Dir-lists the files or dirextories


 


使用可移动的SD


外部存储(SD)卡(Windows Phone8 支持使用SD卡)


<Extensions>


<FileTypeAssociation Name="foo"TaskID="_default" NavUriFragment="fileToken=%s">


<SupportedFileTypes>


<FileTypeContentType="application/foo">.foo</FileType>


</SupportedFileTypes>


</FileTypeAssociation>


</Extensions>


WMAppMainifest.xml中声明文件关联


<Extensions>


<FileTypeAssociation Name="foo"TaskID="_default" NavUriFragment="fileToken=%s">


<SupportedFileTypes>


<FileTypeContentType="application/foo">.foo</FileType>


</SupportedFileTypes>


</FileTypeAssociation>


</Extensions>


最佳方法:


配额管理


Serialization and Threads


 


 


 


0 0
原创粉丝点击