好吧,我已经为这事抗争了几天了,我已经束手无策.我试图通过扩展控件来添加运行时在PropertyGrid中可见的可浏览属性。不管我做什么,iExtenderProvider似乎并没有真正运行。
iExtenderProvider位于第二个项目中,并向主项目添加了一个引用。(代码如下)
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class ControlArray
Inherits Component
Implements IExtenderProvider
<Browsable(True)> Public ReadOnly Property Count As Integer
Get
Return 0
End Get
End Property
Public Function CanExtend(ByVal extendee As Object) As Boolean Implements IExtenderProvider.CanExtend
Return TypeOf extendee Is Control
End Function
End Class然后构建第二个项目,返回到第一个项目,属性窗口中没有任何内容,我在代码中实例化了一个控件,然后尝试找到我的"Count“属性,那里什么也没有。对于这个问题有什么建议吗?
发布于 2016-01-15 01:03:35
在阅读答案之前,
确保你知道:
扩展程序提供程序是向其他组件提供属性的组件。扩展程序提供程序提供的属性实际上驻留在扩展程序提供程序对象本身中,因此不是它修改的组件的真正属性。
在设计时,属性出现在属性窗口中。
在运行时,,但是,您不能通过组件本身访问属性。相反,您可以调用扩展程序组件上的getter和setter方法。
实现扩展程序提供程序
Component继承并实现IExtenderProvider接口。ProvideProperty属性装饰组件类,并引入提供的属性和目标控件类型。CanExtend方法时,为要为其提供属性的每个控件类型返回true。学习更多
示例
使用下面的代码,您可以实现扩展程序组件ControlExtender。当您构建代码并在窗体上放置ControlExtender实例时,它将扩展所有控件,并在属性网格中为控件添加SomeProperty on ControlExtender1属性。
Component并将其命名为ControlExtenderControlExtender.vb中使用这些代码Imports System.ComponentModel
Imports System.Windows.Forms
<ProvideProperty("SomeProperty", GetType(Control))>
Public Class ControlExtender
Inherits Component
Implements IExtenderProvider
Private controls As New Hashtable
Public Function CanExtend(extendee As Object) As Boolean Implements IExtenderProvider.CanExtend
Return TypeOf extendee Is Control
End Function
Public Function GetSomeProperty(control As Control) As String
If controls.ContainsKey(control) Then
Return DirectCast(controls(control), String)
End If
Return Nothing
End Function
Public Sub SetSomeProperty(control As Control, value As String)
If (String.IsNullOrEmpty(value)) Then
controls.Remove(control)
Else
controls(control) = value
End If
End Sub
End Class注意:您还可以根据您的需求继承Control。但在大多数情况下,继承Component更有意义。
https://stackoverflow.com/questions/34802459
复制相似问题