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

如何覆盖Narrator默认播报?自定义类Expander按钮的语音提示

解决Button控件让屏幕阅读器Narrator播报为展开/折叠状态的问题

要让自定义的Button具备Expander的屏幕阅读器播报效果,无需替换成Expander控件,只需通过UI自动化属性修改控件的辅助识别信息,具体实现如下:

  • 给Button设置AutomationProperties.ControlType为Expander,让Narrator将其识别为展开器类型
  • 绑定AutomationProperties.IsExpanded到ViewModel中的展开状态属性,实现状态变化时的播报更新
  • 通过AutomationProperties.Name指定控件的可读名称,比如“信息按钮”

修改后的XAML代码示例:

<Button x:Uid="CopyButton"
        BorderThickness="1"       
        CornerRadius="4"
        Command="{Binding CopyCommand}"
        HorizontalAlignment="Right"
        VerticalAlignment="Bottom"
        AutomationProperties.ControlType="Expander"
        AutomationProperties.Name="信息按钮"
        AutomationProperties.IsExpanded="{Binding IsExpanded}">
    <FontIcon Glyph="{Binding CopyIcon}"
              FontSize="16" />  
</Button>

补充说明:

  1. ViewModel需新增布尔类型属性IsExpanded,并实现INotifyPropertyChanged接口,确保状态变化时通知UI更新
  2. 在CopyCommand的执行逻辑中,要切换IsExpanded的值(例如IsExpanded = !IsExpanded;),这样Narrator会自动播报“信息按钮已展开”或“信息按钮已折叠”
  3. 若使用x:Uid做本地化,可将AutomationProperties.Name的值放到资源文件中,替代硬编码的“信息按钮”

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 23:42:43