【问题标题】:Custom Property, interact with parent class on `set`自定义属性,在 `set` 上与父类交互
【发布时间】:2016-02-14 05:14:29
【问题描述】:

我正在尝试构建自定义属性。

当我设置属性时,我想与父对象中的一个对象进行交互。对于我的生活,我似乎无法弄清楚。我不断收到同样的错误。

“如果没有显式初始化实例,就不能从共享方法或成员中引用类的实例或成员”

Public Class myItem

    Property heading1 As c_heading1

    Private Class c_heading1

        Private newTextValue As String
        Public Property text() As String
            Get
                Return newTextValue
            End Get
            Set(ByVal value As String)
                newTextValue = value
                _set_heading_1_text(value)
            End Set
        End Property

        Private newColorValue As System.Drawing.Color
        Public Property color() As System.Drawing.Color
            Get
                Return newColorValue
            End Get
            Set(ByVal value As System.Drawing.Color)
                newColorValue = value
            End Set
        End Property

    End Class
    Shared Sub _set_heading_1_text(t As String)
        h1.Text = t '///I get the error here, and the same error when I try to access h1 from within the "text" property setter.
    End Sub

End Class

额外信息

不管我在这里付出了多少努力(这可能是完全错误的做法,这就是我真正想要做的事情。)

我有一个用户控件 (uc)。在我的用户控件上,我有一个标签 (l)。

我通常可以使用控件从我的表单中调用 uc.l 。通过这种方式,我可以通过使用用户控件的表单代码直接与标签进行交互。

我现在要做的,本质上是“隐藏”l,(我不希望调用表单能够直接与标签交互)。

我想在 uc 中创建一个可访问的自定义对象,只公开 2 个属性。

当从调用用户控件的表单设置这些属性时,我想从我为自定义对象创建的新属性设置器中设置原始标签的属性。

我可以轻松地在 UC 本身中创建两个属性,一个用于 (l)text,一个用于 (l)color。

但我在 uc 上有多个标签,我想为每个标签引用一个自定义对象,公开特定的自定义属性。

我希望这是有道理的。

【问题讨论】:

  • 如果我从 sub 中删除 shared,我会从调用 sub 的 setter 代码中得到“对非共享成员的引用需要一个实例”。我也不能直接从 setter 代码调用 h1,因为它给了我同样的错误。 h1 是一个标签,它不会调用自定义文本属性。我正在构建一个用户控件,本质上是想使用用户控件从代码中“隐藏”标签及其所有属性,因此是我的自定义属性。
  • 对不起,我误读了 _set_heading 在私有类之外。什么是 H1,它是从哪里来的? heading1.Text = t 创建实例后应该可以工作(没有共享)。
  • h1 是用户控件上的标签。
  • 我认为你误解了我想要做的事情。我可以很好地调用该属性,但我不能做的是与 setter 中的 h1 标签交互。
  • 您是否尝试将属性更改为heading1 滴流到另一个控件/对象? INotifyPropertyChanged 可能会更好地解决这个问题。该代码在其他方面是非法的:您不能让公共属性公开私有类型。该子上的Shared 允许您从私有类中调用它,但作为共享方法,它不能引用其他实例成员(如 H1)

标签: .net vb.net class properties scope


【解决方案1】:

显然,c_heading1 是为了在一定程度上镜像或控制标签。为此,只需使用实际标签作为一些新属性的支持字段:

加上新信息: 我有 3 个标签...我不想创建 6 个属性

Public Class UC

    Public Property HeadingA As HeaderLabel
    Public Property HeadingB As HeaderLabel
    Public Property HeadingC As HeaderLabel

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' important
        HeadingA = New HeaderLabel(Label1)
        HeadingB = New HeaderLabel(Label2)
        HeadingC = New HeaderLabel(Label3)

    End Sub

    Public Class HeaderLabel
        Public Property Text As String
            Get
                Return myLabel.Text
            End Get
            Set(value As String)
                ' do something fabulous
                myLabel.Text = value
            End Set
        End Property

        Public Property Color As Color
            Get
                Return myLabel.BackColor
            End Get
            Set(value As Color)
                ' do soemthing else fabulous
                myLabel.BackColor = value
            End Set
        End Property


        Private myLabel As Label
        Public Sub New(lbl As Label)
            myLabel = lbl
        End Sub

    End Class

End Class

I dont want the calling form to be able to interact with the label directly

如果您在 UserControl 上将 Modifiers 属性设置为 Private,表单将无法再引用它。

用法:

    Uc1.HeadingA.Text = "Something Fabulous"
    Uc1.HeadingA.Color = Color.AliceBlue

【讨论】:

  • 谢谢,我仍然认为我们不在同一个页面上:-)。我有 3 个标签...我不想创建 6 个这样的属性:l1_text, l1_color, l2_text, l2_color, l3_text, l3_color 我想调用 3 个主要属性 (l1,l2,l3),并在每个属性上设置两个字段:(l1.text , l1.color) (l2.text, l2.color) (l3.text, l3.color.)
  • 我想我已经弄清楚我做错了什么(我尝试使用它的方式),很快就会报告。
  • I have 3 labels... and I don;t want to create 6 properties 我们只能通过您提供的任何信息。更新与您所拥有的类似,但没有粘在特定控件上
  • 就是这样!谢谢。
【解决方案2】:

我结束了这个。在这个例子中,我想直接从 _load 调用 .text 和 .color ,但是在 @Plutonix 的帮助下,我认为这不可能吗?

Public Class awsumlistItem

    Private new_heading1 As LabelProperties
    Public Property heading1 As LabelProperties
        Get
            Return new_heading1
        End Get
        Set(ByVal value As LabelProperties)
            new_heading1 = value
            h1.Text = new_heading1.text
            h1.color = new_heading1.color
        End Set
    End Property

    Private new_heading2 As LabelProperties
    Public Property heading2 As LabelProperties
        Get
            Return new_heading2
        End Get
        Set(ByVal value As LabelProperties)
            new_heading2 = value
        End Set
    End Property

    Private new_heading3 As LabelProperties
    Public Property heading3 As LabelProperties
        Get
            Return new_heading3
        End Get
        Set(ByVal value As LabelProperties)
            new_heading3 = value
        End Set
    End Property

    Public Class LabelProperties

        Public Property text() As String
        Public Property color() As System.Drawing.Color

    End Class


    Private Sub awsumlistItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '  Me.Dock = DockStyle.Fill

        Dim myHeading1 As New LabelProperties

        With myHeading1
            .text = "blah blah"
            .color = System.Drawing.Color.Aqua
        End With

        heading1 = myHeading1


    End Sub



End Class

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 2011-06-04
    • 2018-11-28
    • 2014-09-21
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多