Trying to put a 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}" />
In the designer all looks OK but when I run the code it fails with:
Provide value on 'System.Windows.StaticResourceExtension' threw an exception.
I've stared at it for ages but can't spot the problem!
EDIT
It seems to be something to do with the application overall. If I copy my code into another fresh project it works fine. The only difference is that the window is loaded using the "StartupUri="MainWindow.xaml". In the one that doesn't work I load the window up during the App.Startup as follows:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
new TestWindow().Show();
}
EDIT
Found the problem - I was missing an InitializeComponent call. Now the styles work in the final product but not in the designer. I'm going to ask a separate question about it.