问题
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?
回答1:
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 }
来源:https://stackoverflow.com/questions/13396582/wpf-user-control-throws-design-time-exception