开发者

VB.NET Usercontrol with designer browsable property not saving value at run time

I have been making a control and have added a property for a list(of CustomClass). The custom class contains a string, an image and a list of another custom class which contains a string and an image. I have added this property to the designer by adding the browsable attributes and all appears to work correctly with this.

My problem comes when running the program. I can set all of the values within the property and I can go back to them and they are saved, however, as soon as I run the program, the values are wiped out as the property is set to Nothing.

In order to allow my property to be edited and saved, I realize that I must initialize the list and have done so in the accessor method of the property (if the property's value is nothing).

I have modified the values in the designer and placed a breakpoint on all of the items which use the property and it's underlying field, as well as on the property's methods itself. The first breakpoint hit is of it being accessed and the value is always nothing.

I'm not sure where the values are supposed to be stored as I have checked the designer code where it would normally store types such as strings and I have checked the resources to see if they have been saved there (like an image would be). After running the code, the values in the designer are wiped out also.

Some code within the control

    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

The offending code inside 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

The Top Level Class (Category)

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

The nested class (Subitem)

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

Public Class SubItem

    Public Sub New()

    End Sub

    开发者_C百科''' <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

If anyone has any ideas or knowledge of where the values should be stored, I would be amazingly grateful. I don't think I am doing anything particularly complicated.

Resx File. Portion of the items data:

<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>

Complaining about the last line.


The serialization of changes made at design time to your *.Designer.vb file is controlled by the DesignerSerializationVisibility attribute.

It can have one of three possible values:

Hidden: The code generator does not produce code for the object.
Visible: The code generator produces code for the object.
Content: The code generator produces code for the contents of the object, rather than for the object itself.

In your case, you are trying to serialize the contents of a List(Of T), which means that you'll need to tell the code generator to produce code for the contents of the object, rather than for the object itself—the DesignerSerializationVisibility.Content value.

You currently have the value set to Hidden, which prevents the code generator from producing any code for the object whatsoever! The more common mistake is setting it to Visible. While that is almost always what you want for persistence, that's not going to do the job for properties that represent collections (like List(Of T)) containing data that you're interested in serializing.

So, you need to change the declaration of your property to include this:

<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

That should work beautifully. Any change made at design time will be automatically persisted by the designer code generator into your form's associated *.Designer.vb file, and saved along with your project. However, as discussed in the comments, this will not work for changes made during run-time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜