8

I have a userControl which starts a timer. It looks like the XAML designer is trying to call that code, which links to some back-end database stuff. I keep getting an unhanded exception error in the design screen.

Any ideas how I can stop the designer trying to run the code?

4

1 回答 1

14

XAML 设计器将在设计器中加载时调用 UserControl 的构造函数。为了避免这种情况,您可以在 UserControl 构造函数中放置如下 if 条件

if(System.ComponentModel.DesignMode) return;

您也可以通过这种方式检查

if (!System.ComponenyModel.DesignProperties.GetIsInDesignMode(this))
{ // write constructor code here  }
于 2012-11-15T11:53:28.950 回答