嘗試在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