【问题标题】:AutomationProperties.LiveSetting not working in WPF in .NET Framework 4.7.1AutomationProperties.LiveSetting 在 .NET Framework 4.7.1 中的 WPF 中不起作用
【发布时间】:2019-03-12 22:34:07
【问题描述】:

我有一个 TextBlock,我想从屏幕阅读器跟踪该控件,并且每当在代码中为控件设置新值时,屏幕阅读器都应该读出新文本。这在来自 .NET 框架 4.7.1 的 WPF 中可用,MSDN LINK 中提到了这一点。

但对于 AutomationPeer 值,我总是得到 null。我在代码中遗漏了什么?我是否以正确的方式做这件事?请帮忙。

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>

代码

 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);
        }

【问题讨论】:

    标签: wpf accessibility ui-automation


    【解决方案1】:

    使用CreatePeerForElement 方法为TextBlock 创建一个UIElementAutomationPeer

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2017-12-06
      • 2019-03-25
      相关资源
      最近更新 更多