WPF user control throws design-time exception

独自空忆成欢 提交于 2019-12-09 10:07:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!