Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 使用UI自动化框架公开自定义属性_Winforms_Microsoft Ui Automation - Fatal编程技术网

Winforms 使用UI自动化框架公开自定义属性

Winforms 使用UI自动化框架公开自定义属性,winforms,microsoft-ui-automation,Winforms,Microsoft Ui Automation,给定一个非常基本的WinForms自定义/用户控件,使用System.Windows.Automation可以操作自定义控件的内置属性 这是这样做的: public object GetPropertyValue(int propertyId) { if (propertyId == AutomationElementIdentifiers.NameProperty.Id) { return "Hello World

给定一个非常基本的WinForms自定义/用户控件,使用System.Windows.Automation可以操作自定义控件的内置属性

这是这样做的:

public object GetPropertyValue(int propertyId)
      {
         if (propertyId == AutomationElementIdentifiers.NameProperty.Id)
           {
              return "Hello World!";
           }
      }
我想做的是将自定义属性公开给ui自动化,如ReadyState、LastAccess等


这可能吗?

不,您不能扩展属性列表,而且由于您使用的Winforms的UI自动化支持较差(它使用IAccessible with bridges等),这一点变得复杂

但您可以做的是向自动化树中添加一些伪对象,例如,下面是一个示例Winforms UserControl:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        Button button = new Button();
        button.Location = new Point(32, 28);
        button.Size = new Size(75, 23);
        button.Text = "MyButton";
        Controls.Add(button);

        Label label = new Label();
        label.Location = new Point(49, 80);
        label.Size = new Size(35, 13);
        label.Text = "MyLabel";
        Controls.Add(label);

        MyCustomProp = "MyCustomValue";
    }

    public string MyCustomProp { get; set; }

    protected override AccessibleObject CreateAccessibilityInstance()
    {
        return new UserControl1AccessibleObject(this);
    }

    protected class UserControl1AccessibleObject : ControlAccessibleObject
    {
        public UserControl1AccessibleObject(UserControl1 ownerControl)
            : base(ownerControl)
        {
        }

        public new UserControl1 Owner
        {
            get
            {
                return (UserControl1)base.Owner;
            }
        }

        public override int GetChildCount()
        {
            return 1;
        }

        public override AccessibleObject GetChild(int index)
        {
            if (index == 0)
                return new ValueAccessibleObject("MyCustomProp", Owner.MyCustomProp);

            return base.GetChild(index);
        }
    }
}

public class ValueAccessibleObject : AccessibleObject
{
    private string _name;
    private string _value;

    public ValueAccessibleObject(string name, string value)
    {
        _name = name;
        _value = value;
    }

    public override AccessibleRole Role
    {
        get
        {
            return AccessibleRole.Text; // activate Value pattern
        }
    }

    // note you need to override with member values, base value cannot always store something
    public override string Value { get { return _value; } set { _value = value; } }
    public override string Name { get { return _name; } }
}
这是它在自动化树中的显示方式(使用inspect.exe工具):


注意:此技术还支持写回属性,因为它基于ValuePattern。

这是一个很好的答案,只是为了确认CustomProp是否会干扰常规UI元素子结构?您所说的“干扰”是什么意思?它是树中的一个完整元素,它是一个子元素。是否可以将子元素动态添加到myCustProp?只有在您(作为应用程序开发人员)愿意的情况下。同样,您基本上会发回要发回或不想发回的AccessibleObject。@SimonMourier您有WPF的解决方案吗?如果您能提供一个wpf片段,那将是非常好的