提问人:nidzolino 提问时间:10/31/2023 更新时间:10/31/2023 访问量:80
具有所有属性的控件的(反)序列化
(De)serialization of controls with all properties
问:
我正在尝试序列化控件的所有属性,对于初学者,我从 Devexpress 的 TextEdit 控件开始。但是,TextBox 控件也会出现同样的问题。序列化仅针对没有从属项或不是枚举器的属性传递,也就是说,它们可以返回字符串 by (反序列化) 。问题是,有没有人处理过这个问题,有没有比为所有控制属性编写单个规则更简单的方法。我开始写作,但这太难了,即使我考虑到不同的控制(来自不同的制造商),这真的太难了。
Public Class ControlProperties
Public Property Text As String
Public Property BackColor As String
Public Property BorderStyle As Integer
Public Property Size As Size
Public Property Location As Point
Public Property ForeColor As String
Public Property Multiline As Boolean
Public Property AllowDrop As Boolean
Public Property Cursor As String
Public Property RightToLeft As RightToLeft
Public Property Font As SerializableFont
End Class
Public Class SerializableFont
Public Property Name As String
Public Property Size As Single
Public Property Style As FontStyle
Public Sub New()
End Sub
Public Sub New(font As Font)
Name = font.Name
Size = font.Size
Style = font.Style
End Sub
Public Function ToFont() As Font
Return New Font(Name, Size, Style)
End Function
End Class
Public Sub SerializeControlProperties(control As Object, filePath As String)
Dim properties As New ControlProperties()
properties.Text = control.Text
properties.BackColor = ColorTranslator.ToHtml(control.BackColor)
properties.BorderStyle = control.BorderStyle
properties.Size = control.Size
properties.Location = control.Location
properties.ForeColor = ColorTranslator.ToHtml(control.ForeColor)
properties.AllowDrop = control.AllowDrop
properties.Cursor = CursorToString(control.Cursor)
properties.RightToLeft = control.RightToLeft
properties.Font = New SerializableFont(control.Font)
Dim serializer As New XmlSerializer(GetType(ControlProperties))
Using writer As TextWriter = New StreamWriter(filePath)
serializer.Serialize(writer, properties)
End Using
End Sub
<?xml version="1.0" encoding="utf-8"?>
<ControlProperties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Text />
<BackColor>#FFFFFF</BackColor>
<BorderStyle>7</BorderStyle>
<Size>
<Width>100</Width>
<Height>20</Height>
</Size>
<Location>
<X>221</X>
<Y>26</Y>
</Location>
<ForeColor>#282828</ForeColor>
<Multiline>false</Multiline>
<AllowDrop>false</AllowDrop>
<Cursor>System.Windows.Forms.Cursor</Cursor>
<RightToLeft>No</RightToLeft>
<Font>
<Name>Tahoma</Name>
<Size>8.25</Size>
<Style>Regular</Style>
</Font>
</ControlProperties>
答: 暂无答案
评论
Serialization passes only for properties that do not have subordinate items