简体   繁体   English

在设计时如何使UserControl保持惰性?

[英]How to make a UserControl inert at design time?

I have a UserControl that does some cool video capture stuff. 我有一个UserControl,可以执行一些很酷的视频捕获工作。 Now I slapped that control on a form - and it starts working right away, even in design time! 现在,我将该控件放在窗体上-即使在设计时,它也可以立即开始工作! That's very gratifying and all to see it working, but it's kind of annoying at design time, and it slows me down. 这非常令人满足,并且所有人都看到了它的工作原理,但是在设计时有点烦人,这让我慢了下来。 I know that the UserControl is effectively in run-time when it's being used on another form/control at design time, but I'd like to be able to shut it off when it's not in an actual running app. 我知道UserControl在设计时在另一种窗体/控件上使用时实际上是在运行时,但是我希望能够在不在实际运行的应用程序中时将其关闭。

What's the best way to do this? 最好的方法是什么?

Put something like this code in your control: 将类似以下代码的内容放入您的控件中:

if(DesignMode)
   return;

DesignMode is the property that tells you you're running in the designer. DesignMode是告诉您正在设计器中运行的属性。

UserControl has a property called DesignMode - you can just jump out of any of the control logic if this is true. UserControl具有一个称为DesignMode的属性-如果为true,则可以跳出任何控制逻辑。

Alternatively, if the design approach of your project fits it, you could implement binding on it, port all the working logic out to a non-UI controller class, and only set the controller DataSource in the constructor. 或者,如果您的项目的设计方法适合它,则可以在其上实现绑定,将所有工作逻辑移植到非UI控制器类中,并仅在构造函数中设置控制器DataSource。 The designer usually realises it can't anticipate what the instance assigned to it is going to be, so doesn't try. 设计人员通常意识到,它无法预期分配给它的实例是什么,因此不要尝试。

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

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