如何创建一个.NET控件与设计时配置机制?控件、创建一个、机制、NET

2023-09-06 11:07:41 作者:习惯丶不从习惯

我想创建一个控制,允许属性被设置的在设计时的,置位时配置控制(即设置了一堆的属性)。理想情况下,配置是否配置类(仿函数),函数,甚至配置名称(即字符串)的实例将最终执行对控制本身的一些code。这code将被写入使用控制开发商。他们会定义任意数量的配置,然后在设计控制的配置属性将显示可接受的选择或者下拉或允许开发者关键在某些值(例如配置名称)。期望的效果是,设置该属性将立即导致控制重绘的设计师根据其新的属性设置。

I want to create a control which allows a property to be set at design time which when set configures the control (i.e. sets a bunch of properties). Ideally, the configuration whether an instance of a configuration class (a functor), a function, or even a configuration name (i.e. a string) would ultimately execute some code against the control itself. That code would be written by the developers using the control. They would define any number of configurations and then in the designer the Configuration property of the control would display either a dropdown of acceptable choices or allow the developer to key in some value (e.g. the configuration name). The desired effect is that setting the property would immediately cause the control to be redrawn in the designer in light of its new property settings.

Public Property Configuration As String
    Get
        Return m_Configuration
    End Get
    Set(value As String)
        m_Configuration = value
        Dim Configure As Action(Of CustomControl) = Configurations(value)
        Configure(Me) //apply custom code written by user of library
    End Set
End Property

我试图创建和加载一个共享的字典配置(在code以上位使用),但我没能找到一种方法,让字典由开发人员进行加载,这样,他的配置将已在词典中,当在上述属性被设定在设计。它的工作,如果我在定义库本身的结构中,但是这违背了定制的目的

I tried creating and loading a Shared dictionary with configurations (used in the above bit of code), but I wasn't able to find a way to allow the dictionary to be loaded by a developer so that his configurations would be already in the dictionary when the above property was set in the designer. It worked if I defined the configurations in the library itself, but that defeats the purpose of customization.

//Where and how would the developer cause this to be executed...
Configurations.Add("DoSomethingWonderful", AddressOf DoSomethingWonderful)

//...so that it would be in place for use at design time?
Me.CustomControl.Configuration = "DoSomethingWonderful"

我试图使房地产的行动(中CustomControl)和揭露一些全球性行动,但设计师并没有让这个属性进行设置。我曾希望该属性将显示已知行动(中CustomControl)名单的方法。

I tried making the property an Action(Of CustomControl) and exposing some global actions, but the designer didn't allow this property to be set. I had hoped the property would display a list of the known Action(Of CustomControl) methods.

Me.CustomControl.Configuration = AddressOf DoSomethingWonderful

我试图使财产采取的配置类的共享实例(即一个仿函数),但设计师并没有让这个属性被设置无论是。同样,我也希望属性将显示已知的配置实例的列表。

I tried making the property take an Shared instance of a configuration class (i.e. a functor), but the designer didn't allow this property to be set either. Again, I had hoped the property would display a list of known configuration instances.

 Me.CustomControl.Configuration = DoSomethingWonderful 'configuration instance

从自定义控件继承是不是一种选择。继承和覆盖的配置类就可以了。理想情况下,我满足要求最简单的方法后,我。所有这些方法将工作,如果我们等到运行时间,更关键的是,我们有一些在设计时的作品。

Inheriting from the custom control is not an option. Inheriting and overriding a configuration class would be fine. Ideally, I'm after the simplest approach that meets the requirements. All of these approaches would work if we waited until run time, but the key is that we have something that works at design time.

具体而言,我们有一个自定义网格控件在其中,我们定义吨列和设置。我们通常有需要显示的地址电网具有predefined组列和设置。地址表格是用来在几个地方整个应用程序。我们有其他此类网格与用于在应用广泛predefined列和设置。开发商走上复制和粘贴屏幕网格屏幕。这使得作为管理不希望我们继承的基础电网的不同口味(如AddressGrid,PersonGrid等)的感觉。麻烦的是,当我们改变我们显示我们的地址重新配置整个应用中的所有地址的网格。我现在的工作,这样,如果你...

Specifically, we have a custom grid control into which we define tons of columns and settings. We commonly have the need to display an address grid which has a predefined set of columns and settings. The address grid is used in several places throughout the application. We have other such grids with predefined columns and settings that are used in application wide. Developers took to copying and pasting the grid from screen to screen. This makes sense as management would not want us inheriting different flavors of the base grid (e.g. AddressGrid, PersonGrid, etc.). The trouble is when we change how we display addresses we have to reconfigure all the address grids across the app. I have it working now so that if you...

 Me.Addresses.Configuration = "Address"

......整个网格的配置是否正确。唯一的问题是,我们不希望存储的配置在控制库本身。个人应用应定义自己的配置。托管应用程序只需要中继这些配置到库/网格类,以便当上述属性是在设计者设置库识别的配置,并适当地更新设计者的一种手段。

...the entire grid is properly configured. The only issue is that we don't want to store the configurations in the control library itself. The individual applications should be defining their own configurations. The hosting app just needs a means of relaying these configurations to the library/grid class so that when the above property is set in the designer the library recognizes the configuration and updates the designer appropriately.

推荐答案

如果我理解正确的话,你有一个库,包含控制,有一些公共属性,可以在设计中设置。要允许磁带库的用户定义的标准套的属性值,可用于在设计时的控制。

If I understand you correctly, you have a library, that contains control that has a few public properties, that can be set in the designer. You want to allow the consumers of your library to define standard sets of property values, that can be applied to the control at design-time.

您想这样做,因为它是非常重要的,在设计师的控制应该是一样的,在运行时,你不信任你的库的消费者正确适用在每个属性的基础上控制设置

You want to do this because it's of great importance that the controls in the designer should look the same as at runtime, and you don't trust the consumers of your library to apply the control settings correctly on a per-property basis.

有一种内在的悖论,你在问什么,因为你希望能得到您的库运行任意外部code,这可能仍未经过仔细的编制。 Designer配置code其实是由Visual Studio PTED除$ P $。

There's an inherent paradox in what you're asking, because you're hoping to get your library to run arbitrary external code, that might not even be compiled yet. Designer configuration code is actually interpreted by Visual Studio.

有一些控制库(例如: DevEx preSS XtraGrid中),可以保存和使用XML文件恢复布局。这些工作,在设计时要控制的各种属性设置为previously保存的状态。为了实现这样的事情,你必须做一个设计师扩展您的控制。 本文包含有关如何文档。

There are a few control libraries (eg: DevExpress XtraGrid) that can save and restore layout using a XML file. These work at design-time to set the various properties of a control to a previously saved state. In order to implement something like this, you'd have to make a designer extension for your control. This article contains documentation on how.

但在现实中,你只是要经过大量的工作,很少的实际利益。

But in reality, you're just going to go through a lot of work for very little actual benefit.

解决此问题的黑客是只具有片段库,程序员可以直接手工粘贴到窗体设计器生成code。

One hack around this would be to just have a library of snippets that programmers can just manually paste into the Form Designer generated code.

 
精彩推荐
图片推荐