This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
namespace Future.Contract
{
public class Readme : DependencyObject
{
#region 试验各类依赖属性
/// <summary>
/// 注册只读依赖属性
/// </summary>
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyPropertyKey.DependencyProperty); }
protected set { SetValue(IsReadOnlyPropertyKey, value); }
}
// Using a DependencyProperty as the backing store for IsReadOnly. This enables animation, styling, binding, etc...
public static readonly DependencyPropertyKey IsReadOnlyPropertyKey =
DependencyProperty.RegisterReadOnly("IsReadOnly", typeof(bool), typeof(FutureInstruct), new PropertyMetadata(false));
#endregion
#region 参考资料
/*
* WPF气泡弹框
* https://cloud.tencent.com/developer/article/2187533
* WPF自定义窗体
* https://m.jb51.net/program/2972553gk.htm
* MEF框架使用
* https://learn.microsoft.com/zh-cn/dotnet/framework/mef/
* Db4o数据库详解
* https://cloud.tencent.com/developer/article/2107051
* C#文件压缩
* https://m.jb51.net/program/317527fet.htm
* WPF使用SVG格式矢量图
* https://blog.csdn.net/u012563853/article/details/131056882
* */
#endregion
//在WPF或者Silverlight中,ObservableCollection是一个集合,它可以用于绑定UI元素,当集合中的数据发生改变时,UI会自动更新。ObservableCollection实现了INotifyCollectionChanged接口,这使得当集合改变时,它可以通知UI进行更新。
//然而,ObservableCollection本身不支持序列化。如果你需要序列化ObservableCollection,你需要自己实现一些逻辑。
//以下是一种可能的方法:
//使用DataContractSerializer进行序列化。
public static string Serialize<T>(ObservableCollection<T> collection)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(List<T>));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, collection.ToList());
ms.Position = 0;
using (StreamReader reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
public static ObservableCollection<T> Deserialize<T>(string xml)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(List<T>));
using (Stream ms = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(ms))
{
writer.Write(xml);
writer.Flush();
ms.Position = 0;
List<T> list = (List<T>)serializer.ReadObject(ms);
return new ObservableCollection<T>(list);
}
}
}
//使用XmlSerializer进行序列化。
public static string XSerialize<T>(ObservableCollection<T> collection)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
using (StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, collection.ToList());
return textWriter.ToString();
}
}
public static ObservableCollection<T> XDeserialize<T>(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
using (StringReader textReader = new StringReader(xml))
{
List<T> list = (List<T>)serializer.Deserialize(textReader);
return new ObservableCollection<T>(list);
}
}
//SplashScreen:启动屏幕
//SecurityCriticalAttribute:代码安全特性
/*
以上两种方法都是将ObservableCollection先转换为List,然后进行序列化,反序列化的时候,再将List转换回ObservableCollection。
需要注意的是,这两种方法都需要类型T是可序列化的,即类型T上面需要有[DataContract] 或者[XmlType]这样的Attribute。
另外,如果你的ObservableCollection中含有非基础类型,那么这些类型也需要是可序列化的,否则会抛出异常。
以上就是ObservableCollection的序列化方法,希望对你有所帮助。*/
}
}