gpt4 book ai didi

wpf - 为什么我的 WPF 控件的代码隐藏在设计时不运行?

转载 作者:行者123 更新时间:2023-12-04 02:07:23 26 4
gpt4 key购买 nike

我的 WPF UserControl 构造函数中有一些代码,但它似乎没有在设计时执行。

确定后面的代码应该在设计时运行,StackOverflow 上有大量示例代码显示人们使用设计时检查来支持这种情况。

我什至尝试附加一个调试器,但断点没有被击中。

最佳答案

我想我已经在 very old 2007 article on the WPF Designer 中找到了答案(当时称为“苹果酒”)。这是重要的一点:

When you write code behind for a particular control, that code behind may or may not be run at design time.

If the code behind is in the constructor of a control that is hosted by a parent in the Cider designer, it will be run when that control is loaded onto the design surface. For instance, if I create a UserControl named MyUserControl and place it on a Window, the constructor for MyUserControl will be run when that Window is loaded in Cider.

However, if I am designing MyUserControl in Cider, because that type is being modified and created at that time, we don’t instantiate MyUserControl — which in turn means that none of the code behind for MyUserControl will be run.

为了验证这一点,我创建了一个新的 UserControl1.xaml 并放入了一个带有代码隐藏的控件实例,代码隐藏在设计时按预期运行。

关于wpf - 为什么我的 WPF 控件的代码隐藏在设计时不运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42198565/

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