AutomationFocusChangedEvent

I have a problem with AutomationFocusChangedEvent in in February CTP build of WPF. AddAutomationFocusChangedEventHandler works only for Window element. Will this event be supported in the future builds or is there any way to make it working

Thanks.



Answer this question

AutomationFocusChangedEvent

  • SoftLead

    Hello Thomas,

    Adding a reference to UIAutomationClientSideProviders.dll didn't help.

    I still can get AutomationFocusChangedEvent only for Automation elements that have NativeWindowHandle != 0.

    Any idea

    Thanks,

    Vlad


  • Maelia

    Hello Thomas,

    Please see the sample below.
    Thanks,
    Vlad.

    Referenced assemblies:

    UIAutomationClient v3.0.51116.0
    UIAutomationClientsideProviders 3.0.51116.0
    UIAutomationTypes 3.0.51116.0
    WindowsBase 3.0.51116.0
    System 2.0.0.0

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Automation;
    using System.IO;

    namespace FocusApp
    {
    class Program
    {
    private AutomationFocusChangedEventHandler _h;
    private AutomationElement _lastFocused = null;
    public Program()
    {
    _h =
    new AutomationFocusChangedEventHandler(OnFocus);
    }
    static void Main(string[] args)
    {
    Program _instanse = new Program();
    _instanse.Listen();
    }
    public void Listen()
    {
    Automation.AddAutomationFocusChangedEventHandler(_h);
    while(true)
    {
    System.Threading.Thread.Sleep(500);
    AutomationElement el = null;
    try
    {
    el =
    AutomationElement.FocusedElement;
    }
    // An ArgumentException ("hwnd cannot be IntPtr.Zero or null.")
    // will be thrown if focus belongs to some System.Windows.Controls.Control
    catch(Exception e)
    {
    Console.WriteLine("AutomationElement.FocusedElement throwed Exception: {0}", e.Message);
    }
    if (el == null)
    continue;
    if (el.Equals(_lastFocused))
    continue;
    _lastFocused = el;
    Console.WriteLine("Focused Element: Id = {0}; Name = {1}, FrameworkId = {2}",el.Current.AutomationId, el.Current.Name, el.Current.FrameworkId);
    }//while
    }
    // This method is never called if focus passed to some
    // System.Windows.Controls.Control.

    protected void OnFocus(object sender, AutomationFocusChangedEventArgs arg)
    {
    AutomationElement el = sender as AutomationElement;
    Console.WriteLine("Focus changed. Id = {0}; Name = {1}, FrameworkId = {2}",el.Current.AutomationId, el.Current.Name, el.Current.FrameworkId);
    }

    }
    }


  • RicardoJB

    Hello Vlad! I believe you may need to add a reference to UIAutomationClientSideProviders.dll in your project. Basically without that dll you will not get support for Win32/WinForms controls. Can you let me know if that assists with your issue

    Thomas



  • AbhimanyuSirohi

    Do you mind posting the code sample you use with the AutomationFocusChangedEvent

    Thanks,

    Thomas



  • MSFT Johan Stenberg

    Hello Vlad I ran your code against a newer build and it works correctly. Unfortunately the WPF controls in the February CTP were not raising FocusChanged event so this is why you are not seeing support.

    I am working to find out when we can get the new CTP out to you all. As soon as it is available I will let you all know. When it is available this issue will be solved.



  • AutomationFocusChangedEvent