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

UWP全局设置TextBlock前景色(静态资源方式)导致应用崩溃

UWP应用使用StaticResource设置TextBlock前景色崩溃问题

我在Stack Overflow上查阅了大量关于通过StaticResource设置TextBlock前景色的类似问题,但我的UWP应用仍持续崩溃,且未生成任何堆栈跟踪。使用的开发环境是Microsoft Visual Studio Community 2022 v17.2.6。

ThemeColors.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="MyOffWhiteBrush" Color="#eeeeee" />

</ResourceDictionary>

TextBlockStyle.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource MyOffWhiteBrush}" />
    </Style>

</ResourceDictionary>

App.xaml

<Application
    x:Class="TestTextBlock.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestTextBlock">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ThemeColors.xaml" />
                <ResourceDictionary Source="TextBlockStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

MainPage.xaml

<Page
    x:Class="TestTextBlock.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestTextBlock"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <TextBlock Text="helloworld" />
    </Grid>
</Page>

更新#1:
如果将TextBlockStyle.xaml修改为以下内容,应用就能正常运行:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="_" />
    
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource MyOffWhiteBrush}" />
    </Style>

</ResourceDictionary>

完全搞不懂为什么必须声明一个空的SolidColorBrush才能正常运行?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 02:18:07