Trying to put style in app.xaml. My app.xaml reads:
<Application x:Class="TestApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <Style x:Key="TestStyle" TargetType="Button"> <Setter Property="Background" Value="Red"/> </Style> </Application.Resources> </Application>
My XAML for the button is as follows:
<Button Content="Click Me!" Style="{StaticResource TestStyle}" />
Everything looks fine in the designer, but when I run the code, it fails:
Provide value on 'System.Windows.StaticResourceExtension' threw an exception.
I looked at him for ages, but I can not understand the problem!
EDIT
It seems to have something in common with the application. If I copy my code to another new project, it works fine. The only difference is that the window is loaded using βStartupUri =" MainWindow.xaml. βIn what does not work, I load the window during App.Startup as follows:
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new TestWindow().Show(); }
DECISION
Problem detected - I did not have an InitializeComponent call. Styles now work in the final product, but not in the designer. I am going to ask a separate question about this.
source share