1

我正在尝试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。数据序列化器的错误:

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 属性以启用内部成员的序列化 - 有关更多详细信息,请参阅文档。请注意,这样做有一定的安全隐患。

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

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

4

2 回答 2

2

放在[DataMember]公共财产

于 2014-04-01T08:30:28.850 回答
2

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

于 2014-04-01T08:31:13.603 回答