WPF设计不会在启动运行窗口静态构造函数 [英] WPF designer doesn't run window static constructor upon starting up
问题描述
当我加载的解决方案,并有一个开放的设计师标签对一些窗口,那么不执行此窗口静态构造函数。
或许,我的结论是错误的(因为我是绝对无能的设计师负载的东西),但在这里是怎样一个测试用例:
-
创建新的WPF项目
-
创建简单的扩展
公共类MyExtension:的MarkupExtension
{
公共静态布尔测试;
公众覆盖对象ProvideValue(的IServiceProvider的ServiceProvider)=> Test.ToString();
}
- 添加到主窗口
<的TextBlock文本={本地:我}/>
和
静态主窗口()
{
MyExtension.Test = TRUE;
}
- 现在,编译( <大骨节病> F6 骨节病>),
的TextBlock
应显示真
的设计师。 - 请不要关闭设计窗口。关闭VS
- 启动解决方案(双击
SLN
文件)。 - 一旦作为设计师负荷窗口中,您将看到
的TextBlock
显示假
。
跆拳道? ?有人可以证实,(或者是我的VS 2015的bug)
我真的想知道设计师是如何工作的:如何加载窗口,使用哪些事件/方法等它似乎不执行(东西放在那里是不是在设计时发生的),怎么是窗口然后创建并显示窗口构造(非静态的)?
我知道它是如何工作的Visual Studio 2010和读你的问题,我的假设的,同样的原理也适用于Visual Studio的2015年
当一个控件的XAML设计里面呈现(关于VS2010它被称为苹果酒)作为主控制(即窗口
),它的构造是不会运行。在另一边,如果控制是在XAML设计里面呈现的另一个控制的孩子,第一个控制器的构造函数中执行(即用户控件
在窗口
)。你可以阅读更多关于它的这里。
所以,你需要在 My.Test
初始化移动到一个定制控制,例如:
公共类MyTextBlock:TextBlock的
{
静态MyTextBlock()
{
MyExtension.Test = TRUE;
}
}
然后用它里面的窗口
:
<局部:MyTextBlock文本={本地:我}/>
您编译项目后,您将看到设计师的真的文字。我再说一遍:它适用于Visual Studio 2010中,所以我希望它可以代表一个提示来解决您的问题。
When I load solution and there is an opened designer tab for some window, then this window static constructor is not executed.
Perhaps my conclusion is wrong (because I am absolutely clueless how designer load things), but here is a test case:
Create new WPF project.
Create simple extension
public class MyExtension : MarkupExtension
{
public static bool Test;
public override object ProvideValue(IServiceProvider serviceProvider) => Test.ToString();
}
- Add to main window
<TextBlock Text="{local:My}" />
and
static MainWindow()
{
MyExtension.Test = true;
}
- Now compile it (F6),
TextBlock
should showTrue
in designer. - Do not close designer window. Close VS.
- Start solution (double-click
sln
file). - As soon as designer loads window you will see
TextBlock
displayFalse
.
WTF? Can someone confirm that (or is it my VS 2015 bug)?
I would really like to know how designer works: how window is loaded, which events/methods are used, etc. It seems window constructor (non-static one) is not executed (anything put there is not happening in design time), how is the window then created and displayed?
I know how it works for Visual Studio 2010 and reading your question I suppose that the same principles may be also applied to Visual Studio 2015.
When a control is rendered inside the XAML designer (regarding VS2010 it is called Cider ) as the main control (i.e. a Window
) its constructor is not run. On the other side, if a control is a child of another control which is rendered inside the XAML designer, the first control's constructor is executed (i.e. a UserControl
inside a Window
). You can read more about it here.
So you need to move the My.Test
initialization into a customized control, for example:
public class MyTextBlock : TextBlock
{
static MyTextBlock()
{
MyExtension.Test = true;
}
}
Then use it inside your Window
:
<local:MyTextBlock Text="{local:My}" />
After you compile your project, you will see the "True" text in the designer. I repeat: it works for Visual Studio 2010, so I hope it can represent an hint to solve your issue.
这篇关于WPF设计不会在启动运行窗口静态构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!