XAML编译报错:FontImageSource类型未找到Triggers附加属性
问题根因
Triggers 是 VisualElement 类型定义的属性,FontImageSource 不属于可视化元素层级,本身不支持挂载触发器集合,直接编写 <FontImageSource.Triggers> 节点会直接触发编译错误。
修复方法
把触发器逻辑移到外层的 ImageButton 节点上,通过Setter直接给ImageButton.Source属性赋值对应状态的FontImageSource即可,触发器按状态优先级从低到高排列,后声明的触发器会覆盖先声明的状态,刚好匹配消息状态的递进逻辑:未送达→已送达→已读。
修正后的代码:
<ImageButton Padding="0" BackgroundColor="Transparent" HorizontalOptions="End"> <!-- 默认未送达状态 --> <ImageButton.Source> <FontImageSource FontFamily="FontAwesome6Solid" Color="LightGray" Glyph="{x:Static fontAwesome:FontAwesomeIcons.Check}"/> </ImageButton.Source> <ImageButton.Triggers> <!-- 已送达状态:修改图标颜色 --> <DataTrigger TargetType="ImageButton" Binding="{Binding HasDelivered, Source={x:Reference bubble_fdasdl}}" Value="True"> <Setter Property="Source"> <FontImageSource FontFamily="FontAwesome6Solid" Color="MediumAquamarine" Glyph="{x:Static fontAwesome:FontAwesomeIcons.Check}"/> </Setter> </DataTrigger> <!-- 已读状态:切换为双勾图标 --> <DataTrigger TargetType="ImageButton" Binding="{Binding HasBeenRead, Source={x:Reference bubble_fdasdl}}" Value="True"> <Setter Property="Source"> <FontImageSource FontFamily="FontAwesome6Solid" Color="MediumAquamarine" Glyph="{x:Static fontAwesome:FontAwesomeIcons.CheckDouble}"/> </Setter> </DataTrigger> </ImageButton.Triggers> </ImageButton>
如果觉得重复编写FontImageSource的固定属性冗余,可以把三个状态的FontImageSource定义为页面资源,在Setter里直接通过StaticResource引用即可,减少重复代码。
内容的提问来源于stack exchange,提问作者Ryan Kendrick
相关产品推荐
相关产品推荐

