C# 如何使用ResourceDictionary设置窗口样式

C# 如何使用ResourceDictionary设置窗口样式,c#,wpf,xaml,C#,Wpf,Xaml,我创建了一个名为Dictionary1.xaml的ResourceDictionary,下面是代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="{x:Type Windo

我创建了一个名为
Dictionary1.xaml的ResourceDictionary,下面是代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Window}">
    <Setter Property="Background" Value="Red">
    </Setter>
</Style>

我在App.xaml或MainWindow.xaml中引用它,如下所示:

<Application.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>

 <Window.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
 </Window.Resources>


在视图设计器中,窗口的背景变为红色,但当应用程序运行时,它的背景是默认的(白色),为什么?如何操作?

您可以用这种方式设置其他控件的样式,除了
窗口
!尝试为按钮、标签等设置sytle,您将得到正确的结果。要不是一扇窗户,你不会

看看我的另一个答案,它可能会帮助你:

必须设置样式的
x:Key
,并明确设置窗口的样式:

Style=“{DynamicResource key\u name}”

为什么视图设计器显示正确的结果?这可能是一个错误。vs2012/13的xaml设计器有许多错误,您可以在msdn中搜索或提交。我在其中提交了一个错误:

但是微软关闭了它,而且他们最近不会修理它