在独立存储中存储对象列表的问题 [英] problem Storing a list of Objects in Isolated Storage
问题描述
我正在尝试将我创建的对象列表存储在独立存储中,并能够通过自动为它们生成标题来将它们显示在列表中.到目前为止,代码有效,但是一旦我对应用程序进行墓碑删除并启动它,除了对象列表之外,我的所有数据都将被保存.我认为我的问题可能在于我如何初始化列表,或者可能是我如何显示名称.任何帮助表示赞赏.
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.
此代码在我的 App.xaml.cs 中:
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();
}
}
此代码在我的 mainPage 上,用于将项目添加到列表中:
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);
}
}
先谢谢你!
推荐答案
这是我用来从独立存储中保存和加载对象列表的代码.
Here is the code I use to save and load lists of objects from isolated storage.
public class IsoStoreHelper
{
private static IsolatedStorageFile _isoStore;
public static IsolatedStorageFile IsoStore
{
get { return _isoStore ?? (_isoStore = IsolatedStorageFile.GetUserStoreForApplication()); }
}
public static void SaveList<T>(string folderName, string dataName, ObservableCollection<T> dataList) where T : class
{
if (!IsoStore.DirectoryExists(folderName))
{
IsoStore.CreateDirectory(folderName);
}
string fileStreamName = string.Format("{0}\{1}.dat", folderName, dataName);
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileStreamName, FileMode.Create, IsoStore))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
dcs.WriteObject(stream, dataList);
}
}
public static ObservableCollection<T> LoadList<T>(string folderName, string dataName) where T : class
{
ObservableCollection<T> retval = new ObservableCollection<T>();
if (!IsoStore.DirectoryExists(folderName))
{
IsoStore.CreateDirectory(folderName);
}
string fileStreamName = string.Format("{0}\{1}.dat", folderName, dataName);
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileStreamName, FileMode.OpenOrCreate, IsoStore))
{
if (stream.Length > 0)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
retval = dcs.ReadObject(stream) as ObservableCollection<T>;
}
}
return retval;
}
}
这篇关于在独立存储中存储对象列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!