`
ribishuangba
  • 浏览: 290019 次
文章分类
社区版块
存档分类
最新评论

Silverlight - 用户控件与设计时(Design Time)错误

 
阅读更多

用Blend 或VS 2010的设计视图打开一个用户控件,如果出现错误(最常见的是空引用错误),而debug时程序运行正常,那很可能是由于设计时的一些限制造成控件无法在设计器中正常打开。如何避免呢?通过 System.ComponentModel.DesignerProperties.IsInDesignTool 判断当前状态是否是设计时,在设计时避免构造函数与Loaded事件处理方法中执行那些代码即可,例如:

public partial class 
ProductsView 
: UserControl 
{
public ProductsView() {
InitializeComponent();

if (!DesignerProperties .IsInDesignTool) {
//call web services etc.
}
}
}

此外,设计时访问Applicaiton 或者App也会出错,因为它们在设计时始终为null


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics