I have a Windows.Forms component which has a "mySize" property that returns a Size struct. My intention was to have this property calculate the returned mySize automatically based on the size of the component, unless mySize has been explicity set, in which case, return the set value of mySize. Unfortunately, now that I am embedding the component into a form, the Windows Forms designer has decided to start explicitly generating and setting a value for the mySize property, which is screwing me right up.
我有一个Windows.Forms组件,它具有一个返回Size结构的“mySize”属性。我的目的是让这个属性根据组件的大小自动计算返回的mySize,除非mySize已被明确设置,在这种情况下,返回mySize的设置值。不幸的是,现在我将组件嵌入到表单中,Windows窗体设计器决定开始为mySize属性显式生成和设置一个值,这让我搞砸了。
So I need to set a DefaultValue, so that the Designer will go away and leave me alone.
所以我需要设置一个DefaultValue,这样Designer就会消失,让我一个人呆着。
I've read up on answers dealing with System.ComponentModel.DefaultValue, so I know that I must manually set the value for the property in the constructor, but the answers and documentation I have found only deal with setting a DefaultValue of False, a Constant.
我已经阅读了处理System.ComponentModel.DefaultValue的答案,所以我知道我必须在构造函数中手动设置属性的值,但是我发现的答案和文档只涉及设置DefaultValue为False,不变。
A Size struct is not a Constant and so the VB compiler is freaking out, telling me I can't set a Size as the DefaultValue for Property of Type Size because Sizes aren't constants.
Size结构不是一个常量,所以VB编译器吓坏了,告诉我我不能将Size设置为Type Size属性的DefaultValue,因为Sizes不是常量。
This makes my brain hurt.
这让我的大脑受伤。
I can probably code around the problem by making getMySize and setMySize methods instead of using a property, but I'd like to know if there's actually any way to set the default property for a Size.
我可以通过使用getMySize和setMySize方法而不是使用属性来解决问题,但我想知道是否有任何方法可以为Size设置默认属性。
NB: I'm not using mySize as some kind of deranged attempt to override the Size property (which has a DefaultValue of 150x150, so SOMETHING seems to be able to set DefaultValues for Sizes); mySize is just some Size value required by the Class.
注意:我没有使用mySize作为某种疯狂的尝试来覆盖Size属性(其DefaultValue为150x150,因此SOMETHING似乎能够为Sizes设置DefaultValues); mySize只是Class所需的一些Size值。
2 个解决方案
#1
Instead of applying the DefaultValue attribute, write the following two methods:
不要应用DefaultValue属性,而是编写以下两种方法:
bool ShouldSerializemySize() { ... }
void ResetmySize() { ... }
In ShouldSerializemySize, return true if the value should be serialized to code. In ResetmySize, reset the property to its default value.
在ShouldSerializemySize中,如果值应序列化为代码,则返回true。在ResetmySize中,将属性重置为其默认值。
The component designer will automatically pick up these methods via reflection.
组件设计者将通过反射自动获取这些方法。
More info here: http://msdn.microsoft.com/en-us/library/53b8022e(VS.71).aspx
更多信息:http://msdn.microsoft.com/en-us/library/53b8022e(VS.71).aspx
#2
I've noticed that there's actually an specific example given for setting a defaultValue of type Size in the Community Content section of DefaultValue's MSDN page, which suggests using the DefaultValue constructor described on this page.
我注意到在DefaultValue的MSDN页面的社区内容部分中设置了一个类型为Size的defaultValue的实际特定示例,建议使用此页面中描述的DefaultValue构造函数。
Unfortunately, while the example given there is correct, in that it works, it seems to me that the MSDN documentation would not naturally lead anyone to this answer.
不幸的是,尽管那里给出的例子是正确的,但它起作用,在我看来,MSDN文档自然不会引导任何人得到这个答案。
I'm going to set albahari's answer as the answer to this question (because his answer at least makes some kind of sense) and leave this example here for completeness' sake.
我要把阿尔巴哈里的答案设定为这个问题的答案(因为他的回答至少有些意义)并且为了完整性而在这里留下这个例子。
#1
Instead of applying the DefaultValue attribute, write the following two methods:
不要应用DefaultValue属性,而是编写以下两种方法:
bool ShouldSerializemySize() { ... }
void ResetmySize() { ... }
In ShouldSerializemySize, return true if the value should be serialized to code. In ResetmySize, reset the property to its default value.
在ShouldSerializemySize中,如果值应序列化为代码,则返回true。在ResetmySize中,将属性重置为其默认值。
The component designer will automatically pick up these methods via reflection.
组件设计者将通过反射自动获取这些方法。
More info here: http://msdn.microsoft.com/en-us/library/53b8022e(VS.71).aspx
更多信息:http://msdn.microsoft.com/en-us/library/53b8022e(VS.71).aspx
#2
I've noticed that there's actually an specific example given for setting a defaultValue of type Size in the Community Content section of DefaultValue's MSDN page, which suggests using the DefaultValue constructor described on this page.
我注意到在DefaultValue的MSDN页面的社区内容部分中设置了一个类型为Size的defaultValue的实际特定示例,建议使用此页面中描述的DefaultValue构造函数。
Unfortunately, while the example given there is correct, in that it works, it seems to me that the MSDN documentation would not naturally lead anyone to this answer.
不幸的是,尽管那里给出的例子是正确的,但它起作用,在我看来,MSDN文档自然不会引导任何人得到这个答案。
I'm going to set albahari's answer as the answer to this question (because his answer at least makes some kind of sense) and leave this example here for completeness' sake.
我要把阿尔巴哈里的答案设定为这个问题的答案(因为他的回答至少有些意义)并且为了完整性而在这里留下这个例子。