DataContractJsonSerializer错误

时间:2014-08-20 09:52:36

标签: c# datacontractjsonserializer

我正在尝试使用MVVM制作一个简单的项目并保存数据DataContractJsonSerializer。我得到了这个我无法克服的错误:

“SYSTEM.SERVICEMODEL.WEB.NI.DLL中发生'System.Runtime.Serialization.SerializationException'类型的第一次机会异常”

应用程序运行,但不会加载保存的数据。

这是我的代码,我做错了什么?

    private static async void getData()
    {
        await GetDataAsync();            
    }


    private static async Task GetDataAsync()
    {

        var localFolder = ApplicationData.Current.LocalFolder;


        try
        {
           using (Stream stream = await localFolder.OpenStreamForReadAsync(dataSourceFile))
           {
               DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Project>));
               _dataSource._projects = serializer.ReadObject(stream) as ObservableCollection<Project>;
           }
        }
        catch (Exception)
        {
            _dataSource._projects = new ObservableCollection<Project>();
        }
    }



    private static async void saveData()
    {
        await SaveDataAsync();
    }


    private static async Task SaveDataAsync()
    {
        var localFolder = ApplicationData.Current.LocalFolder;

        using (Stream stream = await localFolder.OpenStreamForWriteAsync(dataSourceFile,CreationCollisionOption.OpenIfExists))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Project>));

            serializer.WriteObject(stream, _dataSource._projects);
        }
    }

任何想法都会受到高度赞赏, 谢谢

0 个答案:

没有答案