首页 > 解决方案 > Xamarin 形成可访问性订单

问题描述

我想更改 VoiceOver 和 Talkback 读取视图中元素的顺序。我发现TabIndex应该设置页面中元素的焦点顺序,所以我尝试构建一个简单的示例应用程序,按照我想要的顺序设置不同的 TabIndex,但它似乎只适用于某些元素(如标签、按钮......)。我创建了一个随机元素,如红色 BoxView 并将其设置为可访问元素box2.SetValue(AutomationProperties.IsInAccessibleTreeProperty, true);,但它始终将焦点作为最后一项,不知道为什么。比我想做一些事情来在附加的东西之后在代码中更改可访问性顺序。有什么建议吗?

XAML:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="AccessibilityFocus.MainPage">
    <AbsoluteLayout x:Name="container">
        <!-- Place new controls here -->
        <Label x:Name="label1" Text="Label 1" TabIndex="10"/>
        <BoxView x:Name="box2" BackgroundColor="Red" TabIndex="20"/>
        <Label x:Name="label3" Text="Label 3" TabIndex="30"/>

        <Button x:Name="buttonChange"
                Text="CHANGE"
                Clicked="changeFocus"
                TabIndex="40"/>

    </AbsoluteLayout>
</ContentPage>

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Internals;

namespace AccessibilityFocus
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]


    public partial class MainPage : ContentPage
    {
        IList<View> _views;

        public MainPage()
        {
            InitializeComponent();
            _views = container.Children;

            AbsoluteLayout.SetLayoutBounds(label1, new Rectangle(0.1, 0.2, 0.2, 0.2));
            AbsoluteLayout.SetLayoutFlags(label1, AbsoluteLayoutFlags.All);
            label1.SetValue(AutomationProperties.IsInAccessibleTreeProperty, true);

            AbsoluteLayout.SetLayoutBounds(box2, new Rectangle(0.5, 0.2, 0.2, 0.2));
            AbsoluteLayout.SetLayoutFlags(box2, AbsoluteLayoutFlags.All);

            box2.SetValue(AutomationProperties.IsInAccessibleTreeProperty, true);
            AutomationProperties.SetName(box2, "Red box");

            AbsoluteLayout.SetLayoutBounds(label3, new Rectangle(0.9, 0.2, 0.2, 0.2));
            AbsoluteLayout.SetLayoutFlags(label3, AbsoluteLayoutFlags.All);
            label3.SetValue(AutomationProperties.IsInAccessibleTreeProperty, true);

            AbsoluteLayout.SetLayoutBounds(buttonChange, new Rectangle(0.5, 0.7, 0.5, 0.2));
            AbsoluteLayout.SetLayoutFlags(buttonChange, AbsoluteLayoutFlags.All);

            _views.ForEach(v => Debug.WriteLine($"Indici: {v.TabIndex}"));

        }

        public void changeFocus(object sender, EventArgs e)
        {
            Debug.WriteLine("Cambio focus order");
            //HERE I WOULD LIKE TO DO SOMETHING TO CHANGE THE ACCESSIBILITY ORDER
        }
    }
}

标签: c#xamarinxamarin.formscross-platformaccessibility

解决方案


推荐阅读