WP8上的Windows.Storage为我提供了反序列化异常

时间:2014-04-01 07:43:14

标签: c# windows-phone-8 local-storage observablecollection data-serialization

我试图在Windows手机中使用新方法保存ObservableCollection(Windows.Storage)。我有以下类,它是我想保存的可观察集合的基础:

    [DataContract]
    class SettingsModel : INotifyPropertyChanged
    {

        public SettingsModel()
        { }

        [DataMember]
        private string _TargetIP {get; set;}

        public string TargetIP
        {
            get 
            {
                return _TargetIP;
            }
            set
            {
                _TargetIP = value;
                NotifyPropertyChanged("TargetIP");
            }
        }

        [DataMember]
        private string _TargetADS { get; set; }

        public string TargetADS
        {
            get
            {
                return _TargetADS;
            }
            set
            {
                _TargetADS = value;
                NotifyPropertyChanged("TargetADS");
            }
        }

        [DataMember]
        private string _ClientIP { get; set; }

        public string ClientIP
        {
            get 
            {
                return _ClientIP;
            }
            set
            {
                _ClientIP = value;
                NotifyPropertyChanged("ClientIP");
            }
        }

        [DataMember]
        private string _ClientADS { get; set; }

        public string ClientADS
        {
            get 
            {
                return _ClientADS;
            }
            set
            {
                _ClientADS = value;
                NotifyPropertyChanged("ClientADS");
            }
        }

        #region Notify property changed
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

保存可观察集合的我的代码是:

    public static async void SaveCollection<T>(string FileName, string FileExtension, ObservableCollection<T> Col) where T : class
    {
        // place file extension
        FileName = FileName + "." + FileExtension;

        // creating the file and replace the current file if the file allready exists
        var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync
            (FileName, 
            Windows.Storage.CreationCollisionOption.ReplaceExisting);

        // openup a new stream to the file (write)
        using (var Stream = await file.OpenStreamForWriteAsync())
        {

            // serialize the observable collection to a writable type
            var DataSerializer = new DataContractSerializer(typeof(ObservableCollection<T>),
                new Type[] { typeof(T) });

            // write data
            DataSerializer.WriteObject(Stream, Col);
        }
    }

静态SaveCollection<t>方法的调用:

StorageHandler.SaveCollection<SettingsModel>("TestData", "txt", Data);

其中Data是基于settingsModel的集合。该调用在SaveCollection方法的最后一行给出了错误。数据串化器的错误:

  

类型&#39; System.Security.SecurityException&#39;的例外情况发生在System.Runtime.Serialization.ni.dll但未在用户代码中处理

     

附加信息:集合数据合同类型&#39; System.Collections.ObjectModel.ObservableCollection`1 [[WP_ADS.Model.SettingsModel,WP_ADS,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]& #39;无法反序列化,因为它没有公共无参数构造函数。添加公共无参数构造函数将修复此错误。或者,您可以将其设置为内部,并使用程序集上的InternalsVisibleToAttribute属性以启用内部成员的序列化 - 有关更多详细信息,请参阅文档。请注意,这样做会产生一定的安全隐患。

知道如何解决这个问题吗?

(正如错误所示,我已经尝试添加无参数构造函数,使构造函数内部但都无效)。

2 个答案:

答案 0 :(得分:2)

[DataMember]放在公共财产

答案 1 :(得分:2)

不要忘记将你的课程设置为公开。