C# Windows 8应用程序本地存储
我正在尝试使用C#开发Windows 8应用程序,我需要在本地设置中存储两个列表(字符串和日期时间)C# Windows 8应用程序本地存储,c#,windows,windows-8,windows-runtime,local-storage,C#,Windows,Windows 8,Windows Runtime,Local Storage,我正在尝试使用C#开发Windows 8应用程序,我需要在本地设置中存储两个列表(字符串和日期时间) List<string> names = new List<string>(); List<DateTime> dates = new List<DateTime>(); 但我在存储列表并从保存的设置中取回列表时遇到问题 您可以通过发送几行代码来存储和检索字符串列表和日期时间列表类型的对象(或存储此类数据的其他方法)来提供帮助 谢谢。请尝试以下内
List<string> names = new List<string>();
List<DateTime> dates = new List<DateTime>();
但我在存储列表并从保存的设置中取回列表时遇到问题
您可以通过发送几行代码来存储和检索字符串列表和日期时间列表类型的对象(或存储此类数据的其他方法)来提供帮助
谢谢。请尝试以下内容:
localSettings.Values["names"] = names
localSettings.Values["dates"] = dates
下面是:
dates = (List<DateTime>) localSettings.Values["dates"];
dates=(列表)localSettings.Values[“dates”];
编辑:看起来我错了,你只能这样存储基本类型。因此,您可能必须使用MemoryStream并仅保存其缓冲区,将所有内容序列化为字节[]。请检查此示例,它演示了如何将集合保存到应用程序存储:这里有一个库名为,它使用XML序列化。您可以存储
对象
以及列表
。使用起来也很简单。只要在您的项目中添加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");
}
accountobj=newaccount{Name=“Mario”,姓氏=“Rossi”,年龄=36};
变量存储=新设置();
SaveAsync(“数据”,obj);
从隔离存储加载:
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");
}
public异步void LoadData()
{
变量存储=新设置();
Account obj=await storage.LoadAsync(“数据”);
}
此外,如果要存储列表,请执行以下操作:
将列表保存在独立存储中:
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");
}
List accountList=新列表();
accountList.Add(新帐户(){Name=“Mario”,姓氏=“Rossi”,年龄=36});
accountList.Add(新账户(){Name=“Marco”,姓氏=“Casagrande”,年龄=24});
accountList.Add(新账户(){Name=“Andrea”,姓氏=“Bianchi”,年龄=43});
变量存储=新设置();
SaveAsync(“数据”,accountList);
从独立存储加载列表:
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");
}
public异步void LoadData()
{
变量存储=新设置();
List accountList=await storage.LoadAsync(“数据”);
}
代码似乎正确,但我试过了。应用程序正确启动,但在运行时我收到以下错误:mscorlib.dll中发生“System.exception”类型的异常,但未在用户代码WinRT中处理信息:尝试序列化要写入应用程序数据存储的值时出错其他信息:不支持此类型的数据。如果有此异常的处理程序,程序可能会安全地继续。我也有同样的问题。问题似乎是当试图持久化DateTime时,就像在c中一样,它不是运行时类型(DateTimeOffset是)…似乎方法是将DateTime从和转换为DateTimeOffset…必须有一个更优雅的解决方案。在WP8 IsolatedStorage works中,finevar applicationData=Windows.Storage.applicationData.Current;var localSettings=applicationData.localSettings;