【问题标题】:Windows.Storage on WP8 gives me Deserialize exceptionWP8 上的 Windows.Storage 给了我反序列化异常
【发布时间】:2014-05-11 20:37:44
【问题描述】:

我正在尝试使用 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&lt;t&gt;方法的调用:

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

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

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

【问题讨论】:

  • 只是为了测试,忽略事件属性ChangeEventHandler
  • 你的意思是,注释掉处理程序?
  • 同样的结果,同样的错误,没有通知改变
  • 我的意思是只使用像 [XML ignore] 这样的属性忽略,但我不知道 datacontract 的语法。真的很奇怪。
  • 嗯,你的课不公开,可能是这样

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


【解决方案1】:

[DataMember]放在公共属性上

【讨论】:

    【解决方案2】:

    别忘了将你的课程设为公开。

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 2019-08-22
      • 2016-10-08
      • 2010-10-25
      相关资源
      最近更新 更多