由于我的样式而发生解析异常

Parse Exception occurring due to my Styling

我正在使用 XAML 在 C# 中创建 WPF 应用程序。我查看了 documentation of creating Styles for XAML.

在我实际 运行 我的应用程序之前,它看起来在设计器中正常工作。在 运行 连接我的应用程序后,我收到一个解析异常。查看指示的行和位置,<Style="{StaticResource T}" /> 是导致此错误的原因。删除它可以解决问题,但这需要我做一个我想避免的内联 Style

遇到此问题的 Page 的 XAML 代码如下,对于有关此问题的任何反馈和指导,我将不胜感激。此处不起作用的样式是 x:Key="T" TargetType="Border".

<Page x:Class="NGClient1.Screen1.BE2.WindowBe2Tablet"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:NGClient1.Screen1.BE2"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="WindowBe2Tablet" Width="1024" Height="1280" Background="Black">

    <Grid>

        <Grid>
            <Grid.ColumnDefinitions >
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Image Source="Resources/backdrop.png" Stretch="UniformToFill" Grid.ColumnSpan="2" Grid.RowSpan="3" />

            <StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock Text="Advert Section Here" />
            </StackPanel>

            <Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" BorderBrush="White" BorderThickness="4" Background="Black" Margin="40, 40, 40, 40" Opacity="0.5"/> -->
            <Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource T}" /> <!-- Exception being thrown here, unsure why -->
            
            <StackPanel Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock Text="Lower Section Left Here" />
            </StackPanel>

            <StackPanel Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock Text="Lower Section Right Here" />
            </StackPanel>

        </Grid>
    </Grid>

    <Page.Resources>
    
        <Style x:Key="T" TargetType="Border" > <!-- x:Key causing exception, unsure why -->
            
            <Setter Property="BorderBrush" Value="White"/>
            <Setter Property="BorderThickness" Value="4"/>
            <Setter Property="Background" Value="Black"/>
            <Setter Property="Margin" Value="40, 40, 40, 40"/>
            <Setter Property="Opacity" Value="0.5"/>
            
        </Style>

        <Style TargetType="TextBlock">
            
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            
        </Style>
        
    </Page.Resources>
</Page>

使用 StaticResource 时,在 XAML 中定义和引用资源的顺序很重要。

Provides a value for any XAML property attribute by looking up a reference to an already defined resource. Lookup behavior for that resource is analogous to load-time lookup, which will look for resources that were previously loaded from the markup of the current XAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.

static resource lookup 的文档所述:

Forward references cannot be resolved by a static resource reference.

在您的例子中,资源部分位于页面底部,因此资源将在首次通过 StaticResource 引用后定义。为了解决这个问题,您必须将资源部分移动到引用其中定义的资源之前的位置,或者改用 DynamicResource

<Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Style="{DynamicResource T}"/>

The DynamicResource Markup Extension instead processes a key by creating an expression, and that expression remains unevaluated until the app runs, at which time the expression is evaluated to provide a value.