我正在尝试使用 C# 开发 Windows 8 应用程序,我需要在本地设置中存储两个列表(字符串和日期时间)
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;
但是我在存储列表并从保存的设置中取回它们时遇到问题。
您能否通过发送几行代码来存储和检索字符串列表和日期时间列表类型对象(或其他一些存储此类数据的方法)来提供帮助?
谢谢。
最佳答案
这是一个名为 Windows 8 Isolated storage 的库,它使用 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");
}
另外如果你想存储 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");
}
关于c# - Windows 8 应用程序本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16070308/