WPF designer doesn't run window static constructor upon starting up

老子叫甜甜 提交于 2019-12-06 02:35:02

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.

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