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

在劫持的Style中使用StaticResource导致程序崩溃问题排查

问题原因分析

错误核心是资源类型不匹配:
你定义的ThemedBackground是SolidColorBrush类型资源(从错误信息里的#FF3C3C3C可判断,这是Brush的ARGB字符串格式),但你试图直接将它赋值给SolidColorBrush.Color属性——该属性要求的是Color类型值,而非Brush类型,WPF无法完成类型转换,因此抛出异常。

解决方案

根据ThemedBackground的实际类型,对应两种处理方式:

情况1:ThemedBackground是SolidColorBrush类型(当前场景)

直接复用已有Brush资源即可,无需重新定义MaterialDesignPaper:

<materialDesign:TimePicker.Resources>
    <Style x:Key="MaterialDesignTimePickerTextBox" TargetType="{x:Type materialDesign:TimePickerTextBox}" BasedOn="{StaticResource MaterialDesignTimePickerTextBox}">
        <Setter Property="materialDesign:HintAssist.Background" Value="{StaticResource ThemedBackground}" />
    </Style>
</materialDesign:TimePicker.Resources>

若一定要保留MaterialDesignPaper的定义,可修改为直接引用Brush资源:

<materialDesign:TimePicker.Resources>
    <SolidColorBrush x:Key="MaterialDesignPaper" Brush="{StaticResource ThemedBackground}"/>
    <Style x:Key="MaterialDesignTimePickerTextBox" TargetType="{x:Type materialDesign:TimePickerTextBox}" BasedOn="{StaticResource MaterialDesignTimePickerTextBox}">
        <Setter Property="materialDesign:HintAssist.Background" Value="{DynamicResource MaterialDesignPaper}" />
    </Style>
</materialDesign:TimePicker.Resources>

情况2:ThemedBackground是Color类型

如果ThemedBackground确实是Color类型资源(例如定义为<Color x:Key="ThemedBackground">#3c3c3c</Color>),那你原代码的写法是正确的,此时需要检查ThemedBackground的资源定义,确保它是Color而非Brush类型。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 17:17:14