尝试在app.xaml中添加样式。我的App.xaml中写道:是为什么我的WPF样式不工作? [已解决]
<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>
我的XAML的按钮,如下所示:
<Button Content="Click Me!" Style="{StaticResource TestStyle}" />
在设计所有看起来不错,但是当我运行的代码它失败:
Provide value on 'System.Windows.StaticResourceExtension' threw an exception.
我已经盯着它很久了,但不能发现问题!
编辑
这似乎是与整体应用程序有关。如果我将我的代码复制到另一个新项目中,它工作正常。唯一的区别是,该窗口被使用“的StartupUri =” MainWindow.xaml”装在一个不工作的我期间App.Startup加载窗口组成如下:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
new TestWindow().Show();
}
SOLUTION
发现了问题 - 我是缺少一个InitializeComponent调用现在的风格在最终产品中工作,但不是在设计师,我要问一个单独的问题吧
即将运行但不适用样式。 – 2010-08-06 16:32:24