Windows 8应用本地存储
本文关键字:存储 应用 Windows | 更新日期: 2023-09-27 18:09:37
我正在尝试使用c#开发Windows 8应用程序,我需要在本地设置中存储两个列表(字符串和DateTime)
List<string> names = new List<string>();
List<DateTime> dates = new List<DateTime>();
我使用LocalSettings根据这个页面:http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700361
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
但是当我存储列表并从保存的设置中获取它们时,我有问题。
你可以通过发送几行来存储和检索字符串列表和DateTime列表类型对象(或其他一些方法来存储这类数据)。
谢谢。
这里有一个叫做Windows 8隔离存储的库,它使用XML序列化。你可以存储object
和List<T>
。使用起来也很简单。只要在你的项目中添加DLL,你就有了存储数据的方法。
public class Account
{
public string Name { get; set; }
public string Surname{ get; set; }
public int Age { get; set; }
}
保存在隔离存储:
Account obj = new Account{ Name = "Mario ", Surname = "Rossi", Age = 36 };
var storage = new Setting<Account>();
storage.SaveAsync("data", obj);
从隔离存储加载:
public async void LoadData()
{
var storage = new Setting<Account>();
Account obj = await storage.LoadAsync("data");
}
Also如果你想存储List:在隔离存储中保存列表:
List<Account> accountList = new List<Account>();
accountList.Add(new Account(){ Name = "Mario", Surname = "Rossi", Age = 36 });
accountList.Add(new Account(){ Name = "Marco", Surname = "Casagrande", Age = 24});
accountList.Add(new Account(){ Name = "Andrea", Surname = "Bianchi", Age = 43 });
var storage = new Setting<List<Account>>();
storage.SaveAsync("data", accountList );
从隔离存储加载列表:
public async void LoadData()
{
var storage = new Setting<List<Account>>();
List<Account> accountList = await storage.LoadAsync("data");
}
请查看此示例,它演示了如何将集合保存到应用程序存储:http://code.msdn.microsoft.com/windowsapps/CSWinStoreAppSaveCollection-bed5d6e6
尝试存储:
localSettings.Values["names"] = names
localSettings.Values["dates"] = dates
和这个来读:
dates = (List<DateTime>) localSettings.Values["dates"];
看起来我错了,你只能用这种方式存储基本类型。因此,您可能必须使用MemoryStream将所有内容序列化为byte[],并仅保存其缓冲区。