[英]Error {DynamicResource resourcename}' value is not a valid MarkupExtension expression
[英]WPF my program has "MarkupExtension is not valid for Setter.Value" error but working fine in runtime
仅当我将<ImageSource x:key="...">
图像资源文件</ImageSource>
添加到我的<application.resources>
部分时,才会出现此错误。 但是程序在运行时运行良好,没有任何问题。
之前(原始代码 => 0 错误):
<Application.Resources>
<FontFamily x:Key="NotoBold">
fonts/NotoSerifCJKtc-Bold.otf#Noto Serif CJK TC Bold
</FontFamily>
<Style x:Key="HeaderTitle" TargetType="TextBlock">
<Setter Property="Grid.Column" Value="0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="{StaticResource NotoBold}" />
<Setter Property="Foreground" Value="White" />
</Style>
... ...
... ...
... ...
</Application.Resources>
之后(添加 ImageSource xkey='RedCircle1' => 1 错误):
<Application.Resources>
<FontFamily x:Key="NotoBold">
fonts/NotoSerifCJKtc-Bold.otf#Noto Serif CJK TC Bold
</FontFamily>
<ImageSource x:Key="RedCircle1">
images/sample/circleRed1.png
</ImageSource>
<Style x:Key="HeaderTitle" TargetType="TextBlock">
<Setter Property="Grid.Column" Value="0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="{StaticResource NotoBold}" />
<Setter Property="Foreground" Value="White" />
</Style>
... ...
... ...
... ...
</Application.Resources>
错误提示出现在<Style x:Key="HeaderTitle"... >
上。 如果我将setter value="{StaticResource NotoBold}"更改为其他内容(例如 value="Verdana"),错误就会消失。
错误消息: MarkupExtension 对 Setter.Value 无效。 唯一受支持的 MarkupExtension 类型是 DynamicResourceExtension 和 BindingBase 或派生类型。
更新
My.Net 版本是 4.8.03752,Visual Studio 2019 版本是 16.8.3。 我所有的资源文件(Noto 字体和 circleRed1.png)它们的构建操作都是“资源”。
重新产生问题的步骤。
<application.resources>
部分。 (直到这一步一切都还好,没有出现任何错误消息。)我的临时解决方案:
现在每次调试后,我都必须重新修复所有内容,否则我的 xaml 设计器视图将无法正确显示(某些控件的样式属性不会生效)。
请尝试修改代码如下:
<Setter Property="FontFamily" Value="{DynamicResource NotoBold}" />
在加载 XAML 期间,将解析StaticResource并将其分配给属性,这发生在应用程序实际运行之前。 它只会被分配一次,并且对资源字典的任何更改都会被忽略。
DynamicResource在加载期间将表达式 object 分配给属性,但直到运行时才在向表达式 object 询问值时才实际查找资源。 这会延迟查找资源,直到在运行时需要它。 你可以根据具体情况来分析是使用DynamicResource还是StaticResource
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.