gpt4 book ai didi

c# - WPF 用户控件抛出设计时异常

转载 作者:太空狗 更新时间:2023-10-29 18:20:26 24 4
gpt4 key购买 nike

我有一个启动计时器的 userControl。看起来 XAML 设计器正在尝试调用该代码,该代码链接到某些后端数据库内容。我在设计屏幕中不断收到未处理的异常错误。

有什么想法可以阻止设计人员尝试运行代码吗?

最佳答案

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

if(System.ComponentModel.DesignMode) return;

也可以通过这种方式查询

if (!System.ComponenyModel.DesignProperties.GetIsInDesignMode(this))
{ // write constructor code here }

关于c# - WPF 用户控件抛出设计时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com