在App.xaml中为网格设置样式时,我的WPF UI将消失

在App.xaml中为网格设置样式时,我的WPF UI将消失,wpf,xaml,Wpf,Xaml,我有一个包含图像和一些按钮的网格窗口: <Window x:Class="Wormholes.Views.TitleWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.co

我有一个包含图像和一些按钮的网格窗口:

<Window x:Class="Wormholes.Views.TitleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wormholes"
            xmlns:commands="clr-namespace:Wormholes.Commands"
        mc:Ignorable="d"
        Title="Warning: Weird Wormholes!" Height="450" Width="800" WindowState="Maximized" WindowStyle="None">xi
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="64"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image Source="/Images/Splash.png" Grid.RowSpan="2" Grid.ColumnSpan="3"/>
        <Button Grid.Row="1" Grid.Column="0" Command="commands:Commands.StartCommand">Start</Button>
        <Button Grid.Row="1" Grid.Column="2" Command="commands:Commands.ExitCommand">Exit</Button>
    </Grid>
</Window>
xi
开始
出口
以及App.xaml中的样式:

<Application x:Class="Wormholes.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Wormholes"
             StartupUri="Views/TitleWindow.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type Grid}">
            <Setter Property="Background" Value="Black"/>
        </Style>
    </Application.Resources>
</Application>


但是,当App.xaml中存在这种样式时,当我运行我的应用程序时,窗口一打开,图像和按钮就会消失!如果删除样式,则会显示控件(当然不显示样式)。如果我将样式放在
Window.Resources
中,它会工作,但当然它不会应用于我创建的任何其他视图。如何使App.xaml中的样式正常工作?

是否要应用于所有
网格
控件?如果是这样,只需将您的
App.Xaml
修改为此,即可覆盖以前的
Grid
样式:

<Application x:Class="Wormholes.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:Wormholes"
         StartupUri="Views/TitleWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type Grid}" **BasedOn="{StaticResource {x:Type Grid}}"**>
        <Setter Property="Background" Value="Black"/>
    </Style>
</Application.Resources>

然后在
视图中

<Window x:Class="Wormholes.Views.TitleWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Wormholes"
        xmlns:commands="clr-namespace:Wormholes.Commands"
    mc:Ignorable="d"
    Title="Warning: Weird Wormholes!" Height="450" Width="800" WindowState="Maximized" WindowStyle="None">xi
<Grid Style="{StaticResource YourGridStyleKey}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="64"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Image Source="/Images/Splash.png" Grid.RowSpan="2" Grid.ColumnSpan="3"/>
    <Button Grid.Row="1" Grid.Column="0" Command="commands:Commands.StartCommand">Start</Button>
    <Button Grid.Row="1" Grid.Column="2" Command="commands:Commands.ExitCommand">Exit</Button>
</Grid>
xi
开始
出口

我找到了一个解决方法:将资源放在ResourceDictionary中,并从Window.resources中引用该ResourceDictionary。查看我的答案:)查看我的答案,以获得另一篇可能重复的文章,我希望它适用于所有网格,因此我使用了您的第一个示例;但是,我现在遇到了以下错误:
异常:找不到名为“System.Windows.Controls.Grid”的资源。资源名称区分大小写。
<Window x:Class="Wormholes.Views.TitleWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Wormholes"
        xmlns:commands="clr-namespace:Wormholes.Commands"
    mc:Ignorable="d"
    Title="Warning: Weird Wormholes!" Height="450" Width="800" WindowState="Maximized" WindowStyle="None">xi
<Grid Style="{StaticResource YourGridStyleKey}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="64"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Image Source="/Images/Splash.png" Grid.RowSpan="2" Grid.ColumnSpan="3"/>
    <Button Grid.Row="1" Grid.Column="0" Command="commands:Commands.StartCommand">Start</Button>
    <Button Grid.Row="1" Grid.Column="2" Command="commands:Commands.ExitCommand">Exit</Button>
</Grid>