PhoneApplicationService.Current.State相当于在Windows 8
我要寻找在Windows 8.x中等价类API为PhoneApplicationService.Current.State我们在Windows Phone的API找到。
I am looking for equivalent class in Windows 8.x. API for PhoneApplicationService.Current.State which we found in Windows Phone API.
基本上,我想在会议上与坚持的页面之间的简单对象数据。
或有在Windows 8中任何其他选项,以实现这一目标?
Basically, I am trying to persist simple object data between pages with in session. Or is there any other option in Windows 8 to achieve this?
我将不使用国家推荐,它更容易使用的只是IsolatedStorage为维护会话和页面之间的数据为好,保持简单。结果
的所有数据应保存在 ApplicationData.Current.LocalSettings 或 IsolatedStorageSettings.ApplicationSettings 的情况下,它们就像弦,BOOL,INT简单的对象。
I won't recommend using the State, it's much easier to use just IsolatedStorage for preserving data both between sessions and pages as well, to keep is simple.
All data should be saved in ApplicationData.Current.LocalSettings or IsolatedStorageSettings.ApplicationSettings in case they are simple objects like string, bool, int.
// on Windows 8
// input value
string userName = "John";
// persist data
ApplicationData.Current.LocalSettings.Values["userName"] = userName;
// read back data
string readUserName = ApplicationData.Current.LocalSettings.Values["userName"] as string;
// on Windows Phone 8
// input value
string userName = "John";
// persist data
IsolatedStorageSettings.ApplicationSettings["userName"] = userName;
// read back data
string readUserName = IsolatedStorageSettings.ApplicationSettings["userName"] as string;
像整数列表,字符串等复杂的对象应保存在 ApplicationData.Current .LocalFolder 可能是JSON格式(您需要的NuGet JSON.net包):
Complex objects like lists of ints, strings, etc. should be saved in ApplicationData.Current.LocalFolder possibly in JSON format (you need JSON.net package from NuGet):
// on Windows 8
// input data
int[] value = { 2, 5, 7, 9, 42, 101 };
// persist data
string json = JsonConvert.SerializeObject(value);
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myData.json", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, json);
// read back data
string read = await PathIO.ReadTextAsync("ms-appdata:///local/myData.json");
int[] values = JsonConvert.DeserializeObject<int[]>(read);
// on Windows Phone 8
// input data
int[] value = { 2, 5, 7, 9, 42, 101 };
// persist data
string json = JsonConvert.SerializeObject(value);
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myData.json", CreationCollisionOption.ReplaceExisting);
using (Stream current = await file.OpenStreamForWriteAsync())
{
using (StreamWriter sw = new StreamWriter(current))
{
await sw.WriteAsync(json);
}
}
// read back data
StorageFile file2 = await ApplicationData.Current.LocalFolder.GetFileAsync("myData.json");
string read;
using (Stream stream = await file2.OpenStreamForReadAsync())
{
using (StreamReader streamReader = new StreamReader(stream))
{
read = streamReader.ReadToEnd();
}
}
int[] values = JsonConvert.DeserializeObject<int[]>(read);