如何知道UI自动化客户端正在侦听我的WPF应用程序

如何知道UI自动化客户端正在侦听我的WPF应用程序,wpf,ui-automation,screen-readers,Wpf,Ui Automation,Screen Readers,我想让我的应用程序可以访问(让我的应用程序向屏幕阅读器公开,屏幕阅读器是UI自动化客户端,如“叙述者””) 我得到了一些ContentControl,当它获得键盘焦点时,我会显示一个工具提示(从这个ContentControl的tooltip属性获取)。它是一个以多种方式使用的控件,例如,它可以这样使用: 此ContentControl的Content是一个问号图标图像,工具提示是帮助文本 这是一个概念代码: class AutoTooltipOnFocus : ContentControl {

我想让我的应用程序可以访问(让我的应用程序向屏幕阅读器公开,屏幕阅读器是UI自动化客户端,如“
叙述者”
”)

我得到了一些
ContentControl
,当它获得键盘焦点时,我会显示一个工具提示(从这个
ContentControl
tooltip
属性获取)。它是一个以多种方式使用的控件,例如,它可以这样使用: 此
ContentControl
Content
是一个问号图标图像,
工具提示
是帮助文本

这是一个概念代码:

class AutoTooltipOnFocus : ContentControl
{
    public AutoTooltipOnFocus()
    {
        this.GotKeyboardFocus += OnGotKeyboardFocus;
    }

    private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs keyboardFocusChangedEventArgs)
    {
        bool automationListens = AutomationPeer.ListenerExists(AutomationEvents.AutomationFocusChanged);

        if (automationListens)
        {
            // don't show tooltip because GetNameCore of MyAutoPeer will represent the ToolTip value
            return;
        }

        // show tooltip (by reading the ToolTip value of this AutoTooltipOnFocus)
    }

    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new MyAutoPeer(this);
    }
}

class MyAutoPeer : FrameworkElementAutomationPeer
{
    private AutoTooltipOnFocus _owner;

    public MyAutoPeer(AutoTooltipOnFocus owner)
        : base(owner)
    {

    }

    protected override string GetNameCore()
    {
        return GetToolTipValueFromOwner();
    }

    private string GetToolTipValueFromOwner()
    {
        // just for the simplicity of the example, I return this:
        return ToolTipService.GetToolTip(_owner).ToString();
    }
}
例如,
叙述者
,读取
内容的文本表示形式
(假设图像的属性
AutomationProperties.Name
设置为“帮助图标”),然后说“工具提示:一些帮助文本”

我不想指望所有屏幕阅读器都会阅读工具提示(如果我错误地认为他们中的一些人不阅读工具提示,请纠正我),因此我让我的
GetNameCore
返回
tooltip
内容,以便我知道它一定会被阅读,并且我阻止了工具提示的出现(位于
OnGotKeyboardFocus
handler)以防止重复读取相同的帮助文本


问题是:我以为询问AutomationPeer.ListenerExists(AutomationEvents.AutomationFocusChanged)会告诉我UI automation正在监听我的应用程序,但当“讲述人”未运行时,此方法返回“false”其余时间它返回true,因此当没有人使用screen reader时不会显示工具提示,因此我需要知道如何指示UI automation client是否正在运行并侦听我的应用程序。 也许有一种解决方法,可以向我的自定义
AutomationPeer
添加一些代码


感谢您抽出时间!

但是当“讲述人”未运行时,此方法返回“false”剩下的时候它会返回真值——这不是你所期望的吗?当讲述人不运行时,我想显示工具提示,因为这意味着当前使用我的应用程序的用户可以阅读,并且没有眼睛问题。如果我阻止工具提示出现,则“正常”当用户通过键盘关注图标时,他们不知道图标的含义。我知道你的问题…但是你说AutomationPeer.ListenerExists在讲述人未运行时返回false,而在其他时间返回true。那么这有什么错呢?我不能指望,我需要这个问题始终返回false。每个键盘都关注这个问题,我知道我不能显示一个工具提示,然后在剩下的使用中没有工具提示。