You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF运行时引用XAML StaticResource样式抛出解析异常问题求助

问题根因

WPF运行时对XAML采用自上而下的顺序解析,StaticResource标记扩展会在首次被调用时立即向上遍历逻辑树查找已加载的资源字典。你当前将<Page.Resources>块定义在了所有界面内容的末尾,当解析到<Border Style="{StaticResource T}">行时,x:Key="T"的样式还没有被加载到资源字典中,因此会抛出资源找不到的解析异常。
设计器因为会预加载整页XAML的所有内容再渲染,所以不会触发报错,和运行时的解析逻辑存在差异。

解决方案

方案1(优先推荐,性能最优)

将<Page.Resources>块整体移动到Page根节点的起始位置,放在所有界面内容(即你的<Grid>标签)之前,保证静态资源在被调用前已经完成加载:

<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">
    <!-- 资源定义放在所有内容之前 -->
    <Page.Resources>
        <Style x:Key="T" TargetType="Border" >
            <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>
    <!-- 原有界面内容放在资源定义之后 -->
    <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" Style="{StaticResource T}" />
            <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>

方案2(不调整顺序可选)

如果不想修改资源定义位置,可以将{StaticResource T}替换为{DynamicResource T},动态资源会在运行时延迟查找资源,即使资源定义在调用位置之后也能正常生效,但性能略低于静态资源。

内容的提问来源于stack exchange,提问作者PPascarella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 20:54:02