具有所有属性的控件的(反)序列化

(De)serialization of controls with all properties

提问人:nidzolino 提问时间:10/31/2023 更新时间:10/31/2023 访问量:80

问:

我正在尝试序列化控件的所有属性,对于初学者,我从 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>
vb.net WinForms XML 反序列化

评论

0赞 djv 10/31/2023
该 Xml 是序列化文档吗? 看起来大小和位置有效Serialization passes only for properties that do not have subordinate items
0赞 nidzolino 10/31/2023
是的,这可能就是我:D失败的原因。循环访问属性并将其保存为二进制或文本的最简单方法是什么?
0赞 SSS 10/31/2023
为什么要尝试序列化控件?也许有更好的解决方案。
0赞 nidzolino 10/31/2023
我实际上想将所有属性存储在 skl 中,但我也尝试了 kml 和序列化。原因很简单,我有很多动态形式,而使用 propertigrid 我会影响控件的设计和功能。这样一来,我就避免了繁琐的表单绘制,而当我需要在客户端做一些与设计相关的事情时,在源代码之外工作可以加快我的工作速度。
0赞 SSS 10/31/2023
听起来您正在重新发明 IDE。我认为 WPF 将 XAML 用于其 UI,最好使用它而不是 winforms。

答: 暂无答案