Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF user control throws design-time exception

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?

like image 817
Jay Avatar asked Nov 15 '12 11:11

Jay


1 Answers

XAML designer will call the UserControl's constructor when loading in designer. In order to avoid this you can place a if condition as follows in your UserControl constructor

if(System.ComponentModel.DesignMode) return;

You can also check in this way

if (!System.ComponenyModel.DesignProperties.GetIsInDesignMode(this))
{ // write constructor code here  }
like image 128
ygoku Avatar answered Oct 03 '22 03:10

ygoku