Narrator does not read the text in a TextBlock as expected. Only in the selected TextBox

痴心易碎 提交于 2019-12-24 05:16:14

问题


I have a Visual Studio 2019 solution that contains two projects: a Class Library (Universal Windows) that contains a login Page and a Blank App (Universal Windows) that simply navigates to said login Page.

One of my requirements is that the overall solution must pass an accessibility test. The test involves having Narrator (it must be Narrator) read all of the text on the screen.

My problem is that Narrator only reads the text of the TextBox that has focus when the Page loads. I expected Narrator to read the text in the Image element and TextBlock elements as well. Here is an abridged version of the XAML that I'm using in the Page:

<Page>
    <RelativePanel>
        <Image
             x:Name="Logo"
             AutomationProperties.Name="Logo"/>
        <TextBlock
             x:Name="WelcomeTo"
             x:Uid="WelcomeTo"
             Text="Welcome to" />
        <TextBlock
             x:Name="ServiceName"
             x:Uid="ServiceName"
             Text="Service Name" />
        <TextBox
             x:Name="UserId"
             x:Uid="UserId"
             PlaceholderText="User ID"/>
        <TextBox
             x:Name="Password"
             x:Uid="Password"
             PlaceholderText="Password"/>
        <Button
             x:Name="SignIn"
             x:Uid="SignIn"
             Content="Sign In"/>
        <TextBlock
             x:Name="Footer"
             x:Uid="Footer"
             Text="Footer Text" />
    </RelativePanel>
</Page>

As a sanity test, I removed the two TextBox elements to see if focus was the issue. In that case, Narrator did not say any text from the screen. I also tried changing several of the AutomationProperties fields to no avail (i.e. AccessibilityView="Control", LiveSetting="Assertive", etc). I also used the Inspect tool to verify that all of the TextBoxes appeared in the Control view.

Am I missing something?

Any help or suggestions are greatly appreciated. Thank you!


回答1:


Narrator does not read the text in a as expected. Only in the selected TextBox

If you want to read the text of TextBlock, please turn Narrator scan mode on. And then use direction key to read the TextBlock you selected on the page.

Open Narrator : Ctrl + Win + Enter

Turn scan model on : CapsLock + Spacebar




回答2:


While not necessarily the most elegant solution, this worked for what I needed to do.

I set the AutomationProperties.NameProperty of the UserId TextBox that initially gets focus programmatically to include the TextBlock text I wanted. Since I only wanted the TextBlock text to be read once, I save the initial NameProperty value and set it back when UserId subsequently gets focus.

I updated the UserId in the XAML to the following:

<TextBox
         x:Name="UserId"
         x:Uid="UserId"
         PlaceholderText="User ID"
         GotFocus="UserId_GotFocus"/>

I then update the related C# file as follows:

namespace my_namespace
{
    public sealed partial class LoginPage : Page
    {
        private bool hasFullTextBeenRead = false;
        private object userIdNameProperty = null;

        private void UserId_GotFocus(object sender, RoutedEventArgs e)
        {
            if (hasFullTextBeenRead)
            {
                UserId.SetValue(AutomationProperties.NameProperty, userIdNameProperty);
            }
            else
            {
                userIdNameProperty = UserId.GetValue(AutomationProperties.NameProperty);
                UserId.SetValue(AutomationProperties.NameProperty, 
                    // Add a period so Narrator pauses after reading the logo name.
                    FrameworkElementAutomationPeer.FromElement(Logo)?.GetName() + ". " + 
                    FrameworkElementAutomationPeer.FromElement(WelcomeTo)?.GetName() + " " + 
                    // Add a period so Narrator pauses after reading the service name.
                    FrameworkElementAutomationPeer.FromElement(ServiceName)?.GetName() + ". " + 
                    // Add a period so Narrator pauses after reading the footer.
                    FrameworkElementAutomationPeer.FromElement(Footer)?.GetName() + ". " + 
                    FrameworkElementAutomationPeer.FromElement(UserId)?.GetName());

                hasFullTextBeenRead = true;
            }
        }
    }
}

I hope this helps someone else and I would love to hear better solutions. Thanks!



来源:https://stackoverflow.com/questions/56300706/narrator-does-not-read-the-text-in-a-textblock-as-expected-only-in-the-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!