AccessibleObject implementation for custom control

2020-07-10 06:09发布

问题:

I have a very simple controls library for Windows Forms and I am getting problems to implement accessibility.

I have a very simple Form with a member that contains a list of controls of my library, and I have overriden the CreateAccessibilityInstance:

public partial class Form1 : Form
{
    protected override AccessibleObject CreateAccessibilityInstance()
    {
        return new AccessibleForm(this);
    }

    public MyContainer MyContainer;

    public Form1()
    {
        InitializeComponent();
        MyContainer = new MyContainer();
        MyContainer.Controls.Add(new MyButton());
    }
}

The AccessibleForm class looks like:

public class AccessibleForm: Control.ControlAccessibleObject
{
    private Form1 form1;

    public AccessibleForm(Form1 owner):base(owner)
    {
        this.form1 = owner;
    }

    public override AccessibleObject GetChild(int index)
    {
        return this.form1.MyContainer.Controls[index].AccessibilityObject;
    }

    public override int GetChildCount()
    {
        return this.form1.MyContainer.Controls.Count() ;
    }
}

MyContanier and MyButton classes inherits from BaseControl, they are very easy:

public class BaseControl : Control
{
    protected override AccessibleObject CreateAccessibilityInstance()
    {
        return new AccessibleObject();
    }
}


public class MyContainer:BaseControl
{
    public List<BaseControl> Controls { get; set; }

    public MyContainer()
    {
        this.Controls = new List<BaseControl>();
    }
}

public class MyButton:BaseControl
{        
}

The point is that when I run the UIVerify tool to see if my controls are generating the correct structure I can not see them:

Another point is, that if I modify the GetChild method from AccessibleForm class in this way:

public override AccessibleObject GetChild(int index)
{
    return new AccessibleObject();
    ////return this.form1.MyContainer.Controls[index].AccessibilityObject;
}

I can see a node on the UIVerify:

But modifying the GetChild method to return a custom accessible object it shows me nothing.

Why are not my controls on the tree?

I do not know what I am missing.

回答1:

Override Name,value,Role in AccessibleForm class