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?

fabrosell
  • 614
  • 1
  • 8
  • 19
Jay
  • 2,077
  • 5
  • 24
  • 39

1 Answers1

14

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  }
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
ygoku
  • 179
  • 1
  • 4
  • 6
    You can also check in this way If(!System.ComponenyModel.DesignProperties.GetIsInDesignMode(this)){ // write constructor code here
    }
    – G K Aug 20 '13 at 12:52
  • 1
    You sure about this answer? System.ComponentModel should be a name-space, and this makes DesignMode a class, but there is no such thing? Also UserControl doesn't have a System property either so I'm not entirely sure how this should work. – Adam L. S. Aug 18 '19 at 18:16