一、wp7.1APIs
Isolated Storage Classes
•独立存储类在System.IO.IsolatedStorage命名空间中•IsolatedStorageFile
•表示包含文件和目录的独立存储区
•IsolatedFileStream
•公开独立存储中的文件
Saving Data
private void saveGameToIsolatedStorage(string message)
{
using(IsolatedStorageFile isf =
IsolatedStorageFile.GetUserStoreForApplication())
{
using(IsolatedStorageFileStreamrawStream= isf.CreateFile("MyFile.store"))
{
StreamWriterwriter = newStreamWriter(rawStream);
writer.WriteLine(message); // save the message
writer.Close();
}
}
}
Loading Data
private string loadString()
{
stringresult = null;
using(IsolatedStorageFileisf= IsolatedStorageFile.GetUserStoreForApplication())
{
if(isf.FileExists("Myfile.store")
{
using(IsolatedStorageFileStreamrawStream= isf.OpenFile(filename, System.IO.FileMode.Open)) {
StreamReaderreader = newStreamReader(rawStream);
result = reader.ReadLine();
reader.Close();
}}
}
return result;
}
</pre><pre name="code" class="csharp">下面是ADDpange的部分代码
namespace PhoneApp1
{
public partial class AddPage : PhoneApplicationPage
{
public AddPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (this.State.ContainsKey("IncompleteEntry"))
{
this.logtext.Text = this.State["IncompleteEntry"] as string;
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back &&
e.NavigationMode != System.Windows.Navigation.NavigationMode.Forward)
{
this.State["IncompleteEntry"] = this.logtext.Text;
}
base.OnNavigatedFrom(e);
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
SaveEntry();
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
}
private void cancel_Click(object sender, EventArgs e)
{
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
}
private async void SaveEntry()
{
string timeshow = System.DateTime.Now + System.Environment.NewLine;
App thisapp = App.Current as App;//利用APP属性传递值
thisapp.logdata = FileStorageOperation.LoadFromIsolatedStorage();
thisapp.logdata = thisapp.logdata + timeshow + logtext.Text + System.Environment.NewLine;
FileStorageOperation.SavetoIsolatedStorage(thisapp.logdata);
}
}
}
在APP.XAML.CS加入属性的值
public string logdata
{
set;
get;
}
在显示的页面加入
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
App thisapp = App.Current as App;
thisapp.logdata = FileStorageOperation.LoadFromIsolatedStorage();
showblock.Text = thisapp.logdata;
}