我正在尝试使用C#开发Windows8应用程序,我需要在本地设置中存储两个列表(字符串和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;但是我在存储列表和从保存的设置中取回列表时遇到了问题。
您可以发送几行代码来存储和检索string List和DateTime list类型的对象(或其他存储此类数据的方法)。
谢谢。
发布于 2013-04-18 14:07:11
这里有一个名为Windows 8 Isolated storage的库,它使用XML序列化。您可以像存储List<T>一样存储object。使用起来也非常简单。只需在您的项目中添加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");
}另外,如果您想存储列表:将列表保存在独立存储中:
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");
}发布于 2013-04-18 10:24:09
请检查此示例,它演示了如何将集合保存到应用程序存储中:http://code.msdn.microsoft.com/windowsapps/CSWinStoreAppSaveCollection-bed5d6e6
发布于 2013-04-18 05:15:09
试着这样存储:
localSettings.Values["names"] = names
localSettings.Values["dates"] = dates这是这样的:
dates = (List<DateTime>) localSettings.Values["dates"];编辑:看起来我错了,你只能用这种方式存储基本类型。因此,您可能必须通过使用MemoryStream并仅保存其缓冲区来将所有内容序列化为byte[]。
https://stackoverflow.com/questions/16070308
复制相似问题