4

我一直在制作控件并为(CustomClass)列表添加了一个属性。自定义类包含一个字符串、一个图像和一个包含字符串和图像的另一个自定义类的列表。我已经通过添加可浏览属性将此属性添加到设计器中,并且所有似乎都可以正常工作。

我的问题是在运行程序时出现的。我可以设置属性中的所有值,然后我可以返回它们并保存它们,但是,一旦我运行程序,这些值就会被清除,因为属性设置为 Nothing。

为了允许编辑和保存我的属性,我意识到我必须初始化列表并且已经在属性的访问器方法中完成了(如果属性的值是空的)。

我已经修改了设计器中的值,并在使用该属性的所有项目及其基础字段以及该属性的方法本身上放置了一个断点。第一个断点命中是它被访问并且值总是什么都没有。

我不确定应该将值存储在哪里,因为我已经检查了设计器代码,它通常会存储字符串等类型,并且我已经检查了资源以查看它们是否已保存在那里(就像图像一样) . 运行代码后,设计器中的值也会被清除。

控件内的一些代码

    Private _Items As List(Of Models.Category) = New List(Of Models.Category)

<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Data"), _
Description("The items with sub items that should be displayed"), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Property Items As List(Of Models.Category)
    Get
        'Return _Items
        Return Me.ExpMenu1.Categories
    End Get
    Set(ByVal value As List(Of Models.Category))
        Me.ExpMenu1.Categories = value
        '_Items = value
    End Set
End Property

ExpMenu1 中的违规代码

        ''' <summary>
    ''' Gets or sets the categories.
    ''' </summary>
    ''' <value>The categories.</value>
    Public Property Categories() As List(Of Category)
        Get
            If m_Categories Is Nothing Then
                m_Categories = New List(Of Category)
            End If
            Return m_Categories
        End Get
        Set(ByVal value As List(Of Category))
            m_Categories = value
                       End Set
    End Property

顶级班(类别)

Imports System.Collections.Generic

Namespace ExpandingMenuWPF.Models
    ''' <summary>
    ''' Category class.
    ''' </summary>
    Public Class Category
        ''' <summary>
        ''' Gets or sets the name of the category.
        ''' </summary>
        ''' <value>The name of the category.</value>
        Public Property CategoryName() As String
            Get
                Return _CategoryName
            End Get
            Set(ByVal value As String)
                _CategoryName = value
            End Set
        End Property
        Private _CategoryName As String
    ''' <summary>
    ''' Gets or sets the sub items.
    ''' </summary>
    ''' <value>The Sub Items.</value>
    Public Property SubItems() As List(Of SubItem)
        Get
            Return _SubItems
        End Get
        Set(ByVal value As List(Of SubItem))
            _SubItems = value
        End Set
    End Property
    Private _SubItems As New List(Of SubItem)

    Private _ShowImage As Boolean
    Public Property ShowImage() As Boolean
        Get
            Return _ShowImage
        End Get
        Set(ByVal value As Boolean)
            _ShowImage = value
        End Set
    End Property

    Private _Image As Image
    Public Property CategoryImage() As Image
        Get
            Return _Image
        End Get
        Set(ByVal value As Image)
            _Image = value
        End Set
    End Property


End Class
End Namespace

嵌套类(子项)

Namespace ExpandingMenuWPF.Models
''' <summary>
''' Sub Item class.
''' </summary>

Public Class SubItem

    Public Sub New()

    End Sub

    ''' <summary>
    ''' Gets or sets the title.
    ''' </summary>
    ''' <value>The title.</value>
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property
    Private _Title As String

    Private _ShowImage As Boolean
    Public Property ShowImage() As Boolean
        Get
            Return _ShowImage
        End Get
        Set(ByVal value As Boolean)
            _ShowImage = value
        End Set
    End Property

    Private _Image As Image
    Public Property SubItemImage() As Image
        Get
            Return _Image
        End Get
        Set(ByVal value As Image)
            _Image = value
        End Set
    End Property

End Class
End Namespace

如果有人对应该将值存储在哪里有任何想法或知识,我将不胜感激。我不认为我在做任何特别复杂的事情。

Resx 文件。部分项目数据:

<data name="ExpandingVerticalMenu1.Items" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
    AAEAAAD/////AQAAAAAAAAAMAgAAAEdFeHBhbmRpbmdNZW51V1BGLCBWZXJzaW9uPTEuMC4wLjAsIEN1
    bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAwDAAAAUVN5c3RlbS5EcmF3aW5nLCBWZXJz
    aW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49YjAzZjVmN2YxMWQ1MGEz
    YQUBAAAAIEV4cGFuZGluZ01lbnVXUEYuTW9kZWxzLkNhdGVnb3J5BAAAAA1fQ2F0ZWdvcnlOYW1lCV9T
    dWJJdGVtcwpfU2hvd0ltYWdlBl9JbWFnZQEDAASNAVN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkxp
    c3RgMVtbRXhwYW5kaW5nTWVudVdQRi5Nb2RlbHMuU3ViSXRlbSwgRXhwYW5kaW5nTWVudVdQRiwgVmVy
    c2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGxdXQEUU3lzdGVt
    LkRyYXdpbmcuSW1hZ2UDAAAAAgAAAAYEAAAABENhdDEJBQAAAAAKBAUAAACNAVN5c3RlbS5Db2xsZWN0
    aW9ucy5HZW5lcmljLkxpc3RgMVtbRXhwYW5kaW5nTWVudVdQRi5Nb2RlbHMuU3ViSXRlbSwgRXhwYW5k
    aW5nTWVudVdQRiwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2Vu
    PW51bGxdXQMAAAAGX2l0ZW1zBV9zaXplCF92ZXJzaW9uBAAAIUV4cGFuZGluZ01lbnVXUEYuTW9kZWxz
    LlN1Ykl0ZW1bXQIAAAAICAkGAAAAAQAAAAQAAAAHBgAAAAABAAAABAAAAAQfRXhwYW5kaW5nTWVudVdQ
    Ri5Nb2RlbHMuU3ViSXRlbQIAAAAJBwAAAA0DBQcAAAAfRXhwYW5kaW5nTWVudVdQRi5Nb2RlbHMuU3Vi
    SXRlbQMAAAAGX1RpdGxlCl9TaG93SW1hZ2UGX0ltYWdlAQAEARRTeXN0ZW0uRHJhd2luZy5JbWFnZQMA
    AAACAAAABggAAAAEU3ViMQAKCw==
</value>

抱怨最后一行。

4

1 回答 1

5

*.Designer.vb在设计时对文件所做的更改的序列化由DesignerSerializationVisibility属性控制。

它可以具有三个可能的值之一:

隐藏:代码生成器不为对象生成代码。
Visible:代码生成器为对象生成代码。
内容:代码生成器为对象的内容生成代码,而不是为对象本身生成代码。

在您的情况下,您正在尝试序列化 a 的内容List(Of T),这意味着您需要告诉代码生成器为对象的内容生成代码,而不是为对象本身(DesignerSerializationVisibility.Content值)生成代码。

您当前将值设置为Hidden,这会阻止代码生成器为该对象生成任何代码!更常见的错误是将其设置为Visible. 虽然这几乎总是您想要的持久性,但对于表示包含您有兴趣序列化的数据的集合(如 )的属性,这并不能完成这项工作。List(Of T)

因此,您需要更改您的财产声明以包含以下内容:

<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Data"), _
Description("The items with sub items that should be displayed"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Property Items As List(Of Models.Category)
    Get
        'Return _Items
        Return Me.ExpMenu1.Categories
    End Get
    Set(ByVal value As List(Of Models.Category))
        Me.ExpMenu1.Categories = value
        '_Items = value
    End Set
End Property

那应该很好用。设计时所做的任何更改都将由设计器代码生成器自动保存到表单的关联*.Designer.vb文件中,并与您的项目一起保存。但是,正如评论中所讨论的,这不适用于在运行时所做的更改。

于 2011-08-17T16:04:08.140 回答