繁体   English   中英

WP8上的Windows.Storage给我反序列化异常

[英]Windows.Storage on WP8 gives me Deserialize exception

我正在尝试使用Windows Phone(Windows.Storage)中的新方法保存ObservableCollection 我有以下类,这是我要保存的可观察集合的基础:

    [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方法的最后一行给我一个错误。 dataserializer的错误:

System.Runtime.Serialization.ni.dll中发生类型为'System.Security.SecurityException'的异常,但未在用户代码中处理

附加信息:无法反序列化收集数据协定类型'System.Collections.ObjectModel.ObservableCollection`1 [[WP_ADS.Model.SettingsModel,WP_ADS,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]。没有公共的无参数构造函数。 添加公共的无参数构造函数将解决此错误。 或者,可以将其设置为内部,并在程序集中使用InternalsVisibleToAttribute属性以启用内部成员的序列化-有关更多详细信息,请参见文档。 请注意,这样做有一定的安全隐患。

任何想法如何解决这个问题?

(错误提示,我已经尝试添加无参数构造函数,使该构造函数成为内部结构,但两者均无济于事)。

[DataMember]放在公共属性中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM