C# UI Automation: Current.Name returns empty string

549 Views Asked by At

I want to use System.Windows.Automation in my C# program to report Name property on FocusChangedEvent just like AccEvent.exe does.

I have a simple console app which should display the name of the sender whenever FocusChangedEvent is fired:

Automation.AddAutomationFocusChangedEventHandler((sender, e) => {
    AutomationElement element = sender as AutomationElement;
        if (element != null)
            {
                Console.WriteLine(element.Current.Name);
            }
        });

This mostly works as I expected. Switching my focus between windows and elements within windows shows the name of the focused element. However for a lot of elements the Name property returns empty string ("").

Checking with AccEvent.exe (UIA FocusEvent), the sender name displays fine for the same elements.

How to troubleshoot this? Any direction would be appreciated.

1

There are 1 best solutions below

0
On

I switched to UIAComWrapper in my project and now my results are consistent with AccEvent and it also feels much faster.

Differences between UIAComWrapper and UIAutomationClient are stated in another SO question: Difference between UIaComWrapper and UIAutomationCLient + UIAutomationType