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.
来源:https://stackoverflow.com/questions/34672949/wpf-designer-doesnt-run-window-static-constructor-upon-starting-up