我正在编写一个 WP8 应用程序,并且想在应用程序停用或关闭时保存一些数据。
我已经尝试过新的 WinRT api,但在停用应用程序时无法正常工作:|
编码:
public async Task CacheData()
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
string data = "Hello!";
///WinRT not Working !!!
// Get a reference to the Local Folder
// Create the file in the local folder, or if it already exists, just replace it
StorageFile storageFileIsolated = await localFolder.CreateFileAsync("Data.data", CreationCollisionOption.ReplaceExisting);
Stream writeStream = await storageFileIsolated.OpenStreamForWriteAsync();
using (StreamWriter writer = new StreamWriter(writeStream))
{
await writer.WriteAsync(data);
}
}
}
但使用旧的 api 工作正常
string data = "Hello!";
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream rawStream = isf.CreateFile("Data.store"))
{
StreamWriter writer = new StreamWriter(rawStream);
writer.WriteLine(data); // save the message
writer.Close();
}
}
不知道是什么问题:| !!!
我用这段代码测试了它
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
ViewModel.CacheExchangeRates().Wait();
System.Diagnostics.Debug.WriteLine("deactivated !!!!!");
}
从未打印停用!