简体   繁体   English

当自定义控件达到最终大小时会引发事件吗? (.NET Compact Framework)

[英]Is an event thrown when a custom control gets its final size? (.NET Compact Framework)

I'm developing a Windows Mobile 5.0 or above with .Net Compact Framework 2.0 SP2 and C#. 我正在使用.Net Compact Framework 2.0 SP2和C#开发Windows Mobile 5.0或更高版本。

I have a custom control called PullDownMenu (it inherits from System.Windows.Form.Control). 我有一个名为PullDownMenu的自定义控件(它继承自System.Windows.Form.Control)。 This is the code that I use on a WinForm to show it: 这是我在WinForm上用来显示它的代码:

PullDownMenu menu = new PullDownMenu();
menu.Location = new Point(0, 0);
menu.Dock = DockStyle.Fill;
this.Controls.Add(menu);

I've noticed that it size changes during this process. 我注意到在此过程中它的大小发生了变化。 How can I know when it gets its final size? 我怎么知道它何时达到最终尺寸?

Yes, I know I can check OnResize event but this event it is called several times and I think it can downgrade the performance. 是的,我知道我可以检查OnResize事件,但是此事件被多次调用,我认为它可以降低性能。

Is there an event that is thrown when the control is completely displayed? 当控件完全显示时,会引发事件吗? or when it is added to its parent (on this line: this.Controls.Add(menu); ). 或何时将其添加到其父级(在此行: this.Controls.Add(menu); )。

Any advice? 有什么建议吗?

Some of the controls in the Compact Framework support BeginUpdate and EndUpdate methods which may help this issue. Compact Framework中的某些控件支持可以帮助解决此问题的BeginUpdate和EndUpdate方法。 Using a custom control, I believe that you may have to implement your own methods to support this behavior. 使用自定义控件,我相信您可能必须实现自己的方法来支持此行为。

To improve rendering performance, call the BeginUpdate, update the properties that may change its size, and then call the EndUpdate method. 若要提高渲染性能,请调用BeginUpdate,更新可能更改其大小的属性,然后调用EndUpdate方法。 This will minimize the render calls that are invoked by the resizing. 这将最小化由调整大小调用的渲染调用。

The Control.ParentChanged event/Control.OnParentChanged method should handle notification of being added to a control Control.ParentChanged事件/Control.OnParentChanged方法应处理被添加到控件的通知

I would use the Control.Paint/Control.OnPaint to determine the 'final' size. 我将使用Control.Paint / Control.OnPaint来确定“最终”大小。

Does the compact framework allow a container to SuspendLayout and ResumeLayout? 紧凑框架是否允许容器使用SuspendLayout和ResumeLayout?

If you suspend beforehand, and check it's size after ResumeLayout, it should be it's intended size. 如果您事先挂起,并在ResumeLayout之后检查其大小,则应为预期大小。

To specify default size for a custom control, add the size value to a xmta file in your project. 若要为自定义控件指定默认大小,请将大小值添加到项目中的xmta文件中。 If you do not have one already, right click project and select add new item, then choose the designtime attribute file and name it whatever you want. 如果还没有,请右键单击项目并选择添加新项,然后选择designtime属性文件并根据需要命名。

Add the following code to your xmta file: 将以下代码添加到您的xmta文件中:

<Class Name="MyControlNamespace.MyControlClassName">
    <DesktopCompatible>true</DesktopCompatible>
    <Property Name ="Size">
      <DefaultValue>
        <Type>System.Drawing.Size, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Type>
        <Value>100, 20</Value>
      </DefaultValue>
    </Property>
</Class>

Replace namespace and class with your own, also check that the public key token is correct 用您自己的名称空间和类替换,并检查公钥令牌是否正确

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

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