运行 时间代码无法访问对用户控件 属性 的设计时更改
Design-time changes to user control property are not reachable by run-time code
基本上我正在尝试创建一个继承 PictureBox
的用户控件。我想对新图片框进行的更改之一是添加 Opacity
属性.
除了当我在设计时更改不透明度值时,一切似乎都工作正常,该更改不会影响 运行 期间 属性 的实际值!
这是我的代码:
Private _opacity As Integer
Public Property Opacity() As Integer
Get
Return _opacity
End Get
Set(ByVal value As Integer)
_opacity = value
If Image IsNot Nothing Then
MyBase.Image = ChangeOpacity(_image, value)
End If
End Set
End Property
Private _image As Image
Public Shadows Property Image() As Image
Get
Return _image
End Get
Set(ByVal value As Image)
Dim bmp As Bitmap = value
If bmp IsNot Nothing Then
_image = ChangeOpacity(bmp, Opacity)
Else
_image = bmp
End If
MyBase.Image = _image
End Set
End Property
好吧,我只是使用了不同的方法来避免这个问题。我覆盖了 OnPaint
方法,将 ChangeOpacity
方法的代码移到了它,并删除了阴影 属性: Image
因为我不再需要它了。现在问题消失了。 但是 我仍然很好奇为什么 Opacity
属性 的设计时更改没有保存,为什么现在保存了?!
这是我的新工作代码。它可能对某人有帮助:
Public Class Pic
Inherits PictureBox
Private _opacity As Integer
Public Property Opacity() As Integer
Get
Return _opacity
End Get
Set(ByVal value As Integer)
_opacity = value
End Set
End Property
Protected Overrides Sub OnPaint(pe As PaintEventArgs)
If Image IsNot Nothing Then
Dim Gr As Graphics = pe.Graphics
Dim colormatrix As New ColorMatrix
colormatrix.Matrix33 = Opacity / 100
Dim imgAttribute As New ImageAttributes
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
Gr.DrawImage(Image, New Rectangle(0, 0, Image.Width, Image.Height), 0, 0, Image.Width, Image.Height,
GraphicsUnit.Pixel, imgAttribute)
Else
MyBase.OnPaint(pe)
End If
End Sub
End Class
Yes it's called and used but with the default Opacity value no matter what the design time value is!
。
变量在设计者定义的单一方法中被赋予它们的设计时值。设计者大概先初始化了Image变量。
基本上我正在尝试创建一个继承 PictureBox
的用户控件。我想对新图片框进行的更改之一是添加 Opacity
属性.
除了当我在设计时更改不透明度值时,一切似乎都工作正常,该更改不会影响 运行 期间 属性 的实际值!
这是我的代码:
Private _opacity As Integer
Public Property Opacity() As Integer
Get
Return _opacity
End Get
Set(ByVal value As Integer)
_opacity = value
If Image IsNot Nothing Then
MyBase.Image = ChangeOpacity(_image, value)
End If
End Set
End Property
Private _image As Image
Public Shadows Property Image() As Image
Get
Return _image
End Get
Set(ByVal value As Image)
Dim bmp As Bitmap = value
If bmp IsNot Nothing Then
_image = ChangeOpacity(bmp, Opacity)
Else
_image = bmp
End If
MyBase.Image = _image
End Set
End Property
好吧,我只是使用了不同的方法来避免这个问题。我覆盖了 OnPaint
方法,将 ChangeOpacity
方法的代码移到了它,并删除了阴影 属性: Image
因为我不再需要它了。现在问题消失了。 但是 我仍然很好奇为什么 Opacity
属性 的设计时更改没有保存,为什么现在保存了?!
这是我的新工作代码。它可能对某人有帮助:
Public Class Pic
Inherits PictureBox
Private _opacity As Integer
Public Property Opacity() As Integer
Get
Return _opacity
End Get
Set(ByVal value As Integer)
_opacity = value
End Set
End Property
Protected Overrides Sub OnPaint(pe As PaintEventArgs)
If Image IsNot Nothing Then
Dim Gr As Graphics = pe.Graphics
Dim colormatrix As New ColorMatrix
colormatrix.Matrix33 = Opacity / 100
Dim imgAttribute As New ImageAttributes
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
Gr.DrawImage(Image, New Rectangle(0, 0, Image.Width, Image.Height), 0, 0, Image.Width, Image.Height,
GraphicsUnit.Pixel, imgAttribute)
Else
MyBase.OnPaint(pe)
End If
End Sub
End Class
Yes it's called and used but with the default Opacity value no matter what the design time value is!
。
变量在设计者定义的单一方法中被赋予它们的设计时值。设计者大概先初始化了Image变量。