如何覆盖Narrator默认播报?自定义类Expander按钮的语音提示
要让自定义的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>
补充说明:
- ViewModel需新增布尔类型属性
IsExpanded,并实现INotifyPropertyChanged接口,确保状态变化时通知UI更新 - 在
CopyCommand的执行逻辑中,要切换IsExpanded的值(例如IsExpanded = !IsExpanded;),这样Narrator会自动播报“信息按钮已展开”或“信息按钮已折叠” - 若使用
x:Uid做本地化,可将AutomationProperties.Name的值放到资源文件中,替代硬编码的“信息按钮”
内容的提问来源于stack exchange,提问作者AkritiCodes
相关产品推荐
相关产品推荐

