Windows Phone8开发入门(四)
来源:互联网 发布:h5小黄人接金币源码 编辑:程序博客网 时间:2024/06/04 05:55
Windows Phone 8文件和存储
WP7.1 IsolatedStorage和IsolatedStorageSetting是 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.comStorage(WindowsPhoneRunting)编程
Windows Phone8和Windows8兼容性
在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
- Windows Phone8开发入门(四)
- Windows Phone8 开发入门(二)
- Windows Phone8 开发入门(三)
- windows phone8开发教程
- windows phone8开发资源
- 深入浅出:Windows Phone8应用开发
- Windows phone8 基础篇(三) 常用控件开发
- WINDOWS PHONE8 开发 (教你获得WP8开发者账号)
- 【windows phone8 开发】1.开发环境
- Windows phone8
- 【wp8.1学习入门】windows phone8.1运行时
- 从WINDOWS PHONE8的发布看游戏开发的未来
- Windows Phone8模拟器设置
- Windows Phone8开放性下载
- Windows Phone8模拟器设置
- Windows phone8 基础篇(三)常用布局(一)
- Windows phone8 基础篇(二) XAML简介
- windows phone8.1应用数据存储(一)
- easyUI--layout
- Aeroplane chess - HUD 4405 dp
- UNIX 网络编程 卷1 套接字联网API 第3版 环境搭建
- 工作近一年来的感想
- codeforce 14D 无向图题目
- Windows Phone8开发入门(四)
- 图解CSS的padding,margin,border属性(详细介绍及举例说明)
- Fragment嵌套导致内容不显示问题
- FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.解决办法
- 怎么海淘
- python类型转换、数值操作(收藏)
- hdu 1548 A strange lift(最短路或DFS)
- JavaScript中的隐式类型转换
- PL/SQL下载