Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在独立存储中存储对象列表时出现问题_C#_Windows_List_Windows Phone 7_Stackpanel - Fatal编程技术网

C# 在独立存储中存储对象列表时出现问题

C# 在独立存储中存储对象列表时出现问题,c#,windows,list,windows-phone-7,stackpanel,C#,Windows,List,Windows Phone 7,Stackpanel,我试图存储我在独立存储中创建的对象列表,并能够通过自动为它们生成标题在列表中显示它们。到目前为止,代码是有效的,但一旦我删除应用程序并启动它,我所有的数据都会被保存,除了对象列表。我想我的问题可能是如何初始化列表,或者可能是如何显示名称。感谢您的帮助 此代码位于my App.xaml.cs中: public partial class App : Application { public List<my_type> testList = new List<m

我试图存储我在独立存储中创建的对象列表,并能够通过自动为它们生成标题在列表中显示它们。到目前为止,代码是有效的,但一旦我删除应用程序并启动它,我所有的数据都会被保存,除了对象列表。我想我的问题可能是如何初始化列表,或者可能是如何显示名称。感谢您的帮助

此代码位于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();
        }
     }
此代码用于在不同页面的屏幕上实现标题:

 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=0;i<(Application.Current as App).testList.Count;i++)
{
createateTextBlock((Application.Current as App).testList[i].Title\u ToString(),i);
}
}
私有void CreateATextBlock(字符串标题,int num)
{
testblockname=新文本块();
testblockname.Text=(num+1)+“+”标题;
DrList.Children.Add(testblockname);
}
}

提前谢谢你

下面是我用来保存和加载独立存储中对象列表的代码

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;
    }
}
公共类IsoStoreHelper
{
私有静态隔离存储文件_isoStore;
公共静态隔离存储文件IsoStore
{ 
获取{return _isoStore???(_isoStore=IsolatedStorageFile.GetUserStoreForApplication());}
}
公共静态void存储列表(string folderName、string dataName、ObservableCollection dataList),其中T:class
{
如果(!IsoStore.DirectoryExists(folderName))
{
创建目录(folderName);
}
string fileStreamName=string.Format(“{0}\\{1}.dat”,folderName,dataName);
使用(IsolatedStorageFileStream=新的IsolatedStorageFileStream(fileStreamName,FileMode.Create,IsoStore))
{
DataContractSerializer dcs=新的DataContractSerializer(typeof(ObservableCollection));
dcs.WriteObject(流、数据列表);
}
}
公共静态ObservableCollection加载列表(string folderName,string dataName),其中T:class
{
ObservableCollection retval=新的ObservableCollection();
如果(!IsoStore.DirectoryExists(folderName))
{
创建目录(folderName);
}
string fileStreamName=string.Format(“{0}\\{1}.dat”,folderName,dataName);
使用(IsolatedStorageFileStream=新的IsolatedStorageFileStream(fileStreamName,FileMode.OpenOrCreate,IsoStore))
{
如果(stream.Length>0)
{
DataContractSerializer dcs=新的DataContractSerializer(typeof(ObservableCollection));
retval=dcs.ReadObject(stream)作为ObservableCollection;
}
}
返回返回;
}
}

只需将您的集合(列表)添加到您的IsolatedStorageSettings中,您就可以依靠内置序列化程序(DataContractSerializer)序列化对象以持久化到磁盘

您确定可以正确序列化和反序列化对象吗

自己做这件事可能是最简单的方法

如果你自己这样做,而不是那样做: -DataContractSerializer很慢 -DataContractJsonSerializer更快 -Json.net速度更快 -二进制序列化是最快的


序列化自己时,还应将其保存在一个IsolatedStorage文件中,而不是保存在设置中。这有助于提高性能,也增加了灵活性,有助于调试和测试。

感谢Steve的例子,但我在实现它时遇到了这个错误:“不允许在IsolatedStorageFileStream上执行操作”我想这就是我对folderName的表达方式。下面是我的加载用法:testList=IsoStoreHelper.LoadList(“存储/”,“存储”);删除第一个参数末尾的斜杠。如果这不起作用,请尝试将名称存储更改为其他内容,例如(“会话”、“DrinkSesh”),我已经尝试过了,但仍然会出现错误。似乎只有加载方法失败,而不是保存方法失败。我仍然不知道在哪里申报我的名单。现在它只是在App类的顶部,如下所示:public observetecollection testList=new observetecollection();我为我在silverlight上的愚蠢行为感到抱歉,我只是觉得我在这里做了一些根本错误的事情,因为每次打开或激活应用程序时,它都会创建列表的新实例。我错了吗?实际上我刚刚修复了它!这个问题已经研究了大约一周了。结果证明我做了一些根本错误的事情…我在DrinkSesh类中将我的变量声明为private而不是public。我没有意识到这是必要的,但它与我原来的保存代码一起工作。我会继续摆弄你的方法,因为这是一个更好的方法,但现在至少它的功能!谢谢您的帮助。@SteveChadbourne我在-retval=dcs.ReadObject(stream)as List行中得到System.NullReferenceException;LoadList方法的实现。不知道为什么!!谢谢你的提示,马特。DataContractJsonSerializer是否内置于WP7API中?好像记得不是吗?任何关于二进制序列化的egs?@SteveChadbourne DataContractJsonSerializer位于System.ServiceModel.Web.dll程序集中。
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;
    }
}