Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

accessibility - AutomationProperties.LiveSetting not working in WPF in .NET Framework 4.7.1

I have a TextBlock and I want to track that control from Screen reader and whenever a new value is set to the control in code, the screen reader should readout the new text. This is available in WPF from .NET framework 4.7.1 which is mentioned in the MSDN LINK.

But I am always getting null for the AutomationPeer value. What am I missing in the code? Am I doing it in the right way? Please help.

XMAL

      <Window x:Class="WPFAccessibility.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFAccessibility"
                mc:Ignorable="d"
                Title="WPFAccessibility" Height="450" Width="800">
            <Grid>

                <TextBlock Name="MyTextBlock" AutomationProperties.LiveSetting="Assertive">My initial text</TextBlock>

                <Button Name="Save" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="50,321,0,0" Height="49" Click="Save_Click"/>   

            </Grid>
        </Window>

Code

 private void Save_Click(object sender, RoutedEventArgs e)
        {
            // Setting the MyTextBlock text to some other value and screen 
            // reader should notify to the user
            MyTextBlock.Text = "My changed text";
            var peer = UIElementAutomationPeer.FromElement(MyTextBlock); 
           // I am always getting peer value null 
            peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the CreatePeerForElement method to create a UIElementAutomationPeer for the TextBlock:

private void Save_Click(object sender, RoutedEventArgs e)
{
    MyTextBlock.Text = "My changed text";
    var peer = UIElementAutomationPeer.FromElement(MyTextBlock);
    if (peer == null)
        peer = UIElementAutomationPeer.CreatePeerForElement(MyTextBlock);
    peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...