Windows 8 App本地存储 [英] Windows 8 App Local Storage

查看:90
本文介绍了Windows 8 App本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#开发Windows 8应用程序,并且需要在本地设置中存储两个列表(字符串和DateTime)

I am trying to develop Windows 8 apps using C# and I need to store two list's (string and DateTime) in local settings

List<string> names = new List<string>();
List<DateTime> dates = new List<DateTime>();

我根据此页面为此使用了LocalSettings: http: //msdn.microsoft.com/zh-CN/library/windows/apps/xaml/hh700361

I used LocalSettings for that according to this page: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700361

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

但是我在存储列表并从保存的设置取回列表时遇到了问题.

But I have problems while I am storing Lists and getting back them from saved settings.

通过发送两行代码来存储和检索字符串List和DateTime列表类型对象(或其他用于存储此类数据的方法)是否可以帮助您.

Can you help by sending couple of lines to store and retrieve string List and DateTime list type objects (or some other method to store this kind of data).

谢谢.

推荐答案

这里是一个名为 Windows 8独立存储的库,它使用XML序列化.您可以存储objectList<T>.使用也非常容易.只需将DLL添加到您的项目中,然后您便拥有了用于存储数据的方法.

Here is one libarary called Windows 8 Isolated storage, it uses XML serialization. You can store object as well as List<T>. The usage is also so much easy. Just add DLL in your project ans you have methods for storing the data.

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 If you want to store List : Save a List in Isolated Storage:

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 ); 

从隔离存储中加载列表:

Load a List from Isolated Storage:

public async void LoadData()
{    
    var storage = new Setting<List<Account>>();    
    List<Account> accountList = await storage.LoadAsync("data");    
}

这篇关于Windows 8 App本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆