在独立存储中存储对象列表的问题
我正在尝试存储我在独立存储中创建的对象列表,并能够通过自动为它们生成标题来将它们显示在列表中。到目前为止,代码可以工作,但是一旦我对应用程序进行逻辑删除并启动它,除了对象列表之外,所有数据都会被保存。我认为我的问题可能在于我如何初始化列表,或者可能在于我如何显示名称。任何帮助表示赞赏。
此代码位于我的 App.xaml.cs 中:
public partial class App : Application
{
public List<my_type> testList = new List<my_type>();
void loadvalues()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
List<my_Type> L;
if (settings.TryGetValue<List<DrinkSesh>>("Storage", out L))
{ testList = L; }
}
void savevalues()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Storage"] = testList;
settings.Save();
}
}
此代码位于我的主页上,用于将项目添加到列表中:
(Application.Current as App).testList.Add(new my_type());
此代码用于将标题实现到不同页面上的屏幕上:
public different_class()
{
{
InitializeComponent();
for (i = 0; i < (Application.Current as App).testList.Count; i++)
{
CreateATextBlock((Application.Current as App).testList[i].Title_ToString(), i);
}
}
private void CreateATextBlock(String title,int num)
{
testblockname = new TextBlock();
testblockname.Text = (num + 1) + ". " + title;
DrList.Children.Add(testblockname);
}
}
预先感谢您!
I am trying to store a list of objects I created in the isolated storage and be able to display them in a list by auto generating a title for them. So far the code works but once I tombstone the app and start it up all my data is saved except for the list of objects. I think my problem may be with how I initialize the list in the first place, or possibly how I am displaying the names. Any help is appreciated.
this code is in my App.xaml.cs:
public partial class App : Application
{
public List<my_type> testList = new List<my_type>();
void loadvalues()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
List<my_Type> L;
if (settings.TryGetValue<List<DrinkSesh>>("Storage", out L))
{ testList = L; }
}
void savevalues()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Storage"] = testList;
settings.Save();
}
}
This code is on my mainPage to add the items to the list:
(Application.Current as App).testList.Add(new my_type());
and this code is to implement the titles onto the screen on a different page:
public different_class()
{
{
InitializeComponent();
for (i = 0; i < (Application.Current as App).testList.Count; i++)
{
CreateATextBlock((Application.Current as App).testList[i].Title_ToString(), i);
}
}
private void CreateATextBlock(String title,int num)
{
testblockname = new TextBlock();
testblockname.Text = (num + 1) + ". " + title;
DrList.Children.Add(testblockname);
}
}
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我用来从独立存储中保存和加载对象列表的代码。
Here is the code I use to save and load lists of objects from isolated storage.
只需将集合(列表)添加到isolatedStorageSettings,您就可以依靠内置序列化器(DataContractSerializer)来序列化对象以持久保存到磁盘。
您确定您的对象可以正确序列化和反序列化吗?
自己执行此操作可能是最简单的方法。
如果你自己这样做,而不是那样:
- DataContractSerializer 速度很慢
- DataContractJsonSerializer 更快
- Json.net 仍然更快
- 二进制序列化速度最快。
当自己序列化时,您还应该保存在IsolatedStorageFile 中,而不是保存在设置中。这可以帮助提高性能,还可以增加灵活性,从而有助于调试和测试。
By simply adding your collection (List) to your IsolatedStorageSettings you are relying on the built in serializer (the DataContractSerializer) to serialize your object for persisting to disk.
Are you sure your object can be correctly serialized and deserialized?
Doing this yourself is probably the easiest way to do this.
If you do this yourself, not that:
- DataContractSerializer is slow
- DataContractJsonSerializer is faster
- Json.net is faster still
- Binary serialization is fastest.
When serializing yourself you should also save in an IsolatedStorageFile rahter than in the settings. This can help with performance and also adds flexibility which can aid debugging and testing.