-->

WPF TextBlock Contents not read by screen reader

2019-06-01 08:39发布

问题:

I have an application that contains custom WPF Windows that are used to show a popup similar to the Win32 MessageBox.

As part of the requirements the application must be accessible through Screen Readers, and specifically JAWS. I have had problems getting the screen reader to read out the text in the dialog, but it will read the values in the buttons ok.

The code in the XAML is as follows

<Window x:Class="UserControls.ModalDialog"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:converters="clr-namespace:UserControls.Converters"
         mc:Ignorable="d" 
         d:DesignHeight="160" d:DesignWidth="400" MinHeight="85" MinWidth="400" MaxWidth="400" SizeToContent="Height" Height="Auto"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Title="Popup Dialog">

<StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <converters:DisplayIconToSystemIconConverter x:Key="DisplayIconToSystemIconConverter"/>
        </StackPanel.Resources>
        <Image Source="{Binding IconType, Converter={StaticResource DisplayIconToSystemIconConverter}}" Height="32" Width="32" Margin="0,0,10,0"/>
        <TextBlock Name="TextBlock" Margin="20,10,0,0" TextWrapping="Wrap" Width="350" Foreground="DarkSlateGray" FontSize="10" FontWeight="Normal">
            <Run Text="Some text in the dialog"/>
        </TextBlock>
    </StackPanel>

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,5,0">
        <Button Name="Option1Button" Content="OK" Padding="5,0,5,0" Margin="0,20,5,0" MinWidth="100" IsDefault="True" />
        <Button Cancel Padding="5,0,5,0"  Margin="2,20,10,0" MinWidth="75" IsCancel="True" Visibility="Visible"/>
    </StackPanel>
</StackPanel></Window>

This code displays the popup correctly when called, but the screen reader only reads the title twice.

If I add an empty ListView control into the Window as the next element after the TextBlock, the screen reader correctly reads the dialog text, despite the two controls not being explicitly linked, but I cannot have an extra control like this in the control as it will affect the layout.

Is there a way to get the screen reader to correctly read the TextBlock text without having the list view in the control as well?

回答1:

It seems as though WPF still doesn't provide full support for all screen readers. I have searched online and initially only found unanswered similar questions:

WPF: how to make the Screen reader to read the text from a TextBox

After continuing, I found that there is one reader that does appear to work with WPF: NVDA. To find out more, please view the NVDA Community page. I found this from the following question:

Screenreader (NVDA) only reads WPF Window-Title

Furthermore, it seems as though you will need to set the x:Uid properties (normally reserved for the WPF UI automation) to the string to read out. I found this from the following question:

How-to for making my WPF app screen reader compatible?

Finally, you can find a useful tutorial for providing accessibility in WPF Applications in the How to Code WPF Applications for Accessibility on the Dev Pro website.



回答2:

I was able to fix this by giving a value for the AutomationProperties.HelpText property on the Run of the TextBlock and focusing on the TextBlock once the window had loaded.