具有设计器可浏览属性的VB.NET Usercontrol在运行时不保存值

时间:2011-08-17 13:32:51

标签: .net vb.net visual-studio user-controls visual-studio-designer

我一直在制作一个控件,并为列表(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

嵌套类(Subitem)

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>

抱怨最后一行。

1 个答案:

答案 0 :(得分:3)

在设计时对*.Designer.vb文件进行的更改序列化由DesignerSerializationVisibility attribute控制。

它可以有three possible values之一:

  

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

在您的情况下,您正在尝试序列化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文件中,并与项目一起保存。但是,正如评论中所讨论的,这不适用于运行时期间所做的更改。