如何检测 VS.NET 2003 控件库项目中的设计时间
下面的代码无法按预期检测是否处于设计模式(VS.Net 2003 - 控制库):
if (this.Site != null && this.Site.DesignMode == true)
{
// 设计模式
}
否则
{
// 运行时
}
它用于复杂的用户控件,派生自另一个用户控件并在其上包含其他用户控件。
是否有另一种方法可以检测 VS.NET 2003 中的设计时间或者上面的代码有什么问题?
Code below is not working as expected to detect if it is in design mode (VS.Net 2003 - Control Library):
if (this.Site != null && this.Site.DesignMode == true)
{
// Design Mode
}
else
{
// Run-time
}
It is used in a complex user control, deriving from another user control and including other user controls on it.
Is there another way to detect design time in a VS.NET 2003 or what is the problem with the code above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DesignMode
无法在构造函数内部工作。 一些替代方案(不确定是否适用于 1.1)是if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
或
调用 GetService(typeof(IDesignerHost)) 看看它是否返回一些东西。
我的第一个选择运气更好。
DesignMode
won't work from inside a constructor. Some alternatives (not sure if they work in 1.1) areif (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
or
call
GetService(typeof(IDesignerHost))
and see if it returns something.I've had better luck with the first option.
我想您可以按照 http://west- 中所述使用 HttpContext.Current == null Wind.com/weblog/posts/189.aspx
在 .Net 2.0 中,有 Control.DesignMode (http://msdn.microsoft.com/en-us/library/system.web.ui.control.designmode.aspx)。 我想您有充分的理由继续使用 VS 2003,因此升级可能不适合您。
更新 如果您正在使用 Winforms,则 Component.DesignMode (http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx) 是正确的检查方法。 不过,如果 this.Site.DesignMode 无法正常工作,Component.DesignMode 可能也无法正常工作,因为它完全执行了您正在执行的检查 (Site != null && Site.DesignMode)。
这可能不太可能,但您确定您的基本控件不会覆盖 Site 属性吗?
I guess you could use HttpContext.Current == null as described at http://west-wind.com/weblog/posts/189.aspx
In .Net 2.0 there's Control.DesignMode (http://msdn.microsoft.com/en-us/library/system.web.ui.control.designmode.aspx). I guess you have a good reasons to stay on VS 2003 though, so upgrading might not be an option for you.
Update If you are doing Winforms, Component.DesignMode (http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx) is the right way to check. Though, if this.Site.DesignMode doesn't work properly, Component.DesignMode might not work as well, as it does exactly the check you are doing (Site != null && Site.DesignMode).
This might be a long shot, but are you sure that your base control does not override the Site property?