C# 如何将GridLength序列化为用户设置的一部分?
我有一个自定义类型,它有几个C# 如何将GridLength序列化为用户设置的一部分?,c#,wpf,xml-serialization,C#,Wpf,Xml Serialization,我有一个自定义类型,它有几个GridLength属性。此类型是用户设置的一部分。保存设置后,我的设置文件如下所示: <MavEditor xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <RightSideWidth />
GridLength
属性。此类型是用户设置的一部分。保存设置后,我的设置文件如下所示:
<MavEditor xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RightSideWidth />
<LeftSideWidth />
<BottomSideHeight />
其中RightSideWidth
,LeftSideWidth
和BottomSideHeight
是GridLength
在保存设置之前,我知道它们的值不是
Auto
,但XML序列化程序似乎忽略了这一事实
我做错了什么?我用的是这个又快又脏的解决方案。工作正常,但可能会导致性能问题
public GridLength Height
{
// Create the "GridLength" from the separate properties
get => new GridLength(this.Height_Value, (GridUnitType)Enum.Parse(typeof(GridUnitType), this.Height_GridUnitType));
set
{
// store the "GridLength" properties in separate properties
this.Height_GridUnitType = value.GridUnitType.ToString();
this.Height_Value = value.Value;
}
}
public string Height_GridUnitType { get; set; }
public double Height_Value{ get; set; }
您是在谈论
System.Configuration
中的标准.NET应用程序和用户设置吗?这些功能在GridLength上运行得很好。@Clemens:是的,这是WPF应用程序和标准的用户设置机制。值类型不应该有任何意外。。。但是有:调试器显示,Value
属性具有有意义的值,但是user.config
在序列化之后仍然包含空标记。那么,可能是一个愚蠢的问题,但是您是否调用了Properties.Settings.Default.Save()
?看起来您必须在MaveEditor类中实现IXmlSerializable
接口,并使用GridLengthConverter
反序列化GridLength。