Windows 8应用漫游使用自定义类存储 [英] windows 8 app roaming storage with custom class
问题描述
我学习用的C#Windows 8编程。 // MSDN:我经历了很多教程(如 HTTP工作。 microsoft.com/en-us/library/windows/apps/hh986968.aspx )的过程中,我试图创建一个显示数据存储的简单的应用程序。所有的我已经能够找到店里只有简单的字符串存储漫游的例子。是否有存储更复杂的数据有什么办法?
I am learning programming in windows 8 with c#. I have worked through many tutorials (such as http://msdn.microsoft.com/en-us/library/windows/apps/hh986968.aspx) in the process and I am attempting to create a simple app showing data storage. All of the examples I have been able to find store only simple strings in roaming storage. Is there a way to store more complex data there?
例如:一个基本的类人用一个姓名和年龄的名单。我试图做到这一点的:
example: a List of a basic class Person with a name and age. I attempted to do it as:
保存数据:
roamingSettings.Values [peopleList] =人;
roamingSettings.Values["peopleList"] = people;
加载数据:
人=(列表)roamingSettings.Values [peopleList];
people = (List)roamingSettings.Values["peopleList"];
WinRT的信息:试图序列的值错误被写入应用程序数据存储。
保存数据时出现错误这种类型的数据不支持
WinRT information: Error trying to serialize the value to be written to the application data store. when saving the data I get the error "Data of this type is not supported"
所以,也许你可以保存为字符串值 - 但我还没有看到,无论是在任何地方指定。
So, maybe all you can save is string values -- but I have not seen that specified anywhere either.
推荐答案
是的,你可以保存你的价值观,以raoming数据的集合。你的问题的解决方案是
ApplicationDataCompositeValue类
Yes, you can save your values to raoming data as a collection. The solution for your problem is
ApplicationDataCompositeValue class
正如你所说,你是C#开发,下面是你的问题
代码我想象,你有一个Person类有两个成员
As you mentioned, You are developing in C# , following is the code for your problem I imagined, you have a Person class with two members
class person
{
int PersonID;
string PersonName
}
现在,阅读和这个类写入值,这里是代码
Now, to read and write values for this class, here is the code
首先在窗口类的构造函数,在InitializeComponent(下);,创建漫游设置
First in the constructor of your Window class, under the InitializeComponent();, create an object of roaming settings
Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
要写入的组合,使用下面的代码
To Write to a composition, use the following code
void write (Person Peopleobj)
{
Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
composite["PersonID"] = Peopleobj.PersonID;
composite["PersonName"] = Peopleobj.PersonName;
roamingSettings.Values["classperson"] = composite;
}
要读懂一个人的对象,请使用以下代码
To Read a Person object, use the following code
void DisplayOutput()
{
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)roamingSettings.Values["classperson"];
if (composite == null)
{
// "Composite Setting: <empty>";
}
else
{
Peopleobj.PersonID = composite["PersonID"] ;
Peopleobj.PersonName = composite["PersonName"];
}
}
这篇关于Windows 8应用漫游使用自定义类存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!