AutomationProperties.LiveSetting 在 .NET Framework 4.7.1 中的 WPF 中不起作用 [英] AutomationProperties.LiveSetting not working in WPF in .NET Framework 4.7.1

查看:35
本文介绍了AutomationProperties.LiveSetting 在 .NET Framework 4.7.1 中的 WPF 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextBlock,我想从屏幕阅读器跟踪该控件,每当在代码中为控件设置新值时,屏幕阅读器都应读出新文本.这在 .NET 框架 4.7.1 的 WPF 中可用,在 MSDN 链接.

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.

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

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>

代码

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

推荐答案

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

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

这篇关于AutomationProperties.LiveSetting 在 .NET Framework 4.7.1 中的 WPF 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆