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

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

我正在尝试保存 ObservableCollection在 Windows Phone (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 的最后一行给我一个错误方法。 dataserializer的错误:

An exception of type 'System.Security.SecurityException' occurred in System.Runtime.Serialization.ni.dll but was not handled in user code

Additional information: The collection data contract type 'System.Collections.ObjectModel.ObservableCollection`1[[WP_ADS.Model.SettingsModel, WP_ADS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot be deserialized because it does not have a public parameterless constructor. Adding a public parameterless constructor will fix this error. Alternatively, you can make it internal, and use the InternalsVisibleToAttribute attribute on your assembly in order to enable serialization of internal members - see documentation for more details. Be aware that doing so has certain security implications.

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

(如错误所示,我已经尝试添加无参数构造函数,使构造函数成为内部构造函数,但都无济于事)。

最佳答案

不要忘记将您的类(class)设置为公开。

关于c# - WP8 上的 Windows.Storage 给我反序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779658/

相关文章:

javascript - iOS 上的 Safari >=11 默认情况下阻止 cookie 和网络存储 - 如何保留(第一方)数据?

javascript - HTML5本地存储支持的数据

javascript - 显示所有key未知的本地存储

c# - Entity Framework -如何提高任何属性的OnChanging?

c# - lambda 表达式作为属性

C# 异步,没有任务等待

c# - 占位符文本框 Windows Phone 8

javascript - Windows Phone IE Mobile - 如何禁止创建自动 URL(地址和电话号码)?

c# - 在 Converter 中显示 StorageItem 缩略图

c# - MVC : Problems to pass currency in decimal parameter