如何设置Xamarin的VoiceOver不读出对应视图的控件类型?
问题成因
VoiceOver朗读的内容默认由「自定义无障碍描述 + 控件原生类型/特征」拼接生成。你当前使用Image控件模拟按钮功能,系统默认识别到它的原生类型是图片,就会自动追加Image后缀。其他场景朗读Padding等内容同理,是系统识别到了控件的原生类型、布局属性后自动追加的描述。
解决方案
方案1:修改无障碍控件类型(跨平台通用,优先使用)
Xamarin.Forms 提供了AutomationProperties.ControlType附加属性,你可以直接将用作按钮的Image的控件类型指定为按钮,系统就会按按钮的规则朗读,不会再追加Image后缀:
// 在原有设置无障碍属性的代码后添加此行即可 AutomationProperties.SetControlType(someKindOfButton, ControlType.Button);
如果是完全不需要朗读任何控件类型的场景,可以根据需求指定对应类型,比如要纯文本朗读就设置为ControlType.Text,系统就不会追加多余的类型描述。
方案2:原生侧覆盖无障碍标签(适合特殊定制场景)
如果跨平台属性设置无法满足需求,可以对应平台编写自定义渲染器,直接覆盖原生的无障碍朗读内容,完全自定义朗读文本。
以iOS端为例,Image控件的自定义渲染器示例:
[assembly: ExportRenderer(typeof(Image), typeof(CustomImageRenderer))] namespace YourProjectNamespace.iOS { public class CustomImageRenderer : ImageRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Image> e) { base.OnElementChanged(e); if (Control != null) { // 直接指定AccessibilityLabel为自定义的朗读内容,系统不会自动追加任何后缀 Control.AccessibilityLabel = AutomationProperties.GetName(Element); } } } }
方案3:分场景控制开关
你可以封装统一的无障碍设置扩展方法,新增参数控制是否需要朗读控件类型:
public static void SetCustomAutomationProperties(this VisualElement element, string automationId, string name, bool needAnnounceControlType = true) { element.SetAutomationProperties(automationId, name); if (!needAnnounceControlType) { AutomationProperties.SetControlType(element, ControlType.Text); } }
需要隐藏类型后缀的场景,调用扩展方法时传入needAnnounceControlType: false即可。
内容的提问来源于stack exchange,提问作者Maritn Ge
相关产品推荐
相关产品推荐

