简体   繁体   English

避免将Control的默认属性包含在设计时序列化中

[英]Keep Control's default property from being included into Design-time serialisation

I have UserControl, created half in VS Designer and half in code. 我有UserControl,在VS Designer中创建了一半,在代码中创建了一半。 I want to create new instance of this UserControl from code, but set some of its default properties to different values every time. 我想通过代码创建此UserControl的新实例,但每次都将其某些默认属性设置为不同的值。
Example (edit w real code): 示例(用真实代码编辑):

// in my Main Form..
var newFrame = new RPictureFrame(); // my UserControl
newFrame.Width = 50;
newFrame.Height = 50;
newFrame.Location = new Point(0,0);
this.Controls.Add(newFrame);

newFrame = new RPictureFrame();
newFrame.Width = 200;
newFrame.Height = 400;
newFrame.Location = new Point(0,150);
this.Controls.Add(newFrame);

Original wrong example: 原始的错误示例:

var newMyUserControl1 = new MyUserControl();  
newMyUserControl1.size.Width = 50;  

var newMyUserControl2 = new MyUserControl();  
newMyUserControl2.size.Width = 200;

What sometimes happen though is the new value set by me right after creating the new instance of control seem to get ignored. 虽然有时发生的是我在创建控件的新实例后立即设置的新值似乎被忽略了。
My only explanation is, that since my UserControl is designed in Designer, all its default properties are in fact kept serialised in .resx file (?). 我唯一的解释是,由于我的UserControl是在Designer中设计的,因此它的所有默认属性实际上都保留在.resx文件(?)中。 When I create new instance in runtime, a deserialisation is started in separate thread (I have no control of). 当我在运行时创建新实例时,反序列化在单独的线程中启动(我无法控制)。 When I set some of the properties accidentaly before they get deserialised, my value gets overwriten by the ser'd value right after. 当我在反序列化某些属性之前意外设置它们时,我的值将立即被ser'd值覆盖。 I didn't find any info about how the creation of Designer-created controls work, so this is of course just my guess. 我没有找到有关由Designer创建的控件的创建工作方式的任何信息,因此,这当然只是我的猜测。

My question is, how to get around it ? 我的问题是,如何解决它? Possibly some easy way. 可能是一些简单的方法。

The things I tried: 我尝试过的事情:

1- In my UserControl code: 1-在我的UserControl代码中:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Size size;

..to remove the standard properties from Design-time serialisation. ..从设计时序列化中删除标准属性。 But (compiler says) I'm only hiding the existing standard property. 但是(编译器说)我只是隐藏现有的标准属性。 Plus it of course doesn't work. 加上它当然是行不通的。 I am still a beginner in c# & .NET so maybe I'm just doing it wrong ? 我仍然是C#和.NET的初学者,所以也许我只是做错了?

2- I think I can subscribe to each new control's (on)Load event, let it fully deserialize, and then set what I need. 2-我想我可以订阅每个新控件的(on)Load事件,让它完全反序列化,然后设置我需要的内容。 But it seems very cumbersome in code, the properties will needlessly get set twice (or more) etc. Basically I don't believe there isn't more elegant solution :) 但是在代码中看起来非常麻烦,属性将被不必要地设置两次(或更多),等等。基本上我不相信没有更优雅的解决方案了:)

Will be glad for any advices 任何建议都会很高兴

Updated Answer: 更新的答案:

Based on your new code: 根据您的新代码:

newFrame.Width = 50;
newFrame.Height = 50;

Change that to: 更改为:

newFrame.Size = new Size(50, 50);

You'll notice that pattern is being used for Location as well. 您会注意到该模式也用于定位。

Original Answer: 原始答案:

When changing the size of a control you have to da new Size variable, since it's a value type. 更改控件的大小时,您必须设置一个新的Size变量,因为它是一个值类型。

Your code should be: 您的代码应为:

newMyUserControl1.size = new Size(50, newMyUserControl1.size.Height);

Check out this link: 查看此链接:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.size.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.size.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM