AccessibleObject.Name 属性

定义

获取或设置对象名。

C#
public virtual string Name { get; set; }
C#
public virtual string? Name { get; set; }

属性值

对象名,或者,如果尚未设置属性,则是 null

例外

无法检索或设置控件名。

示例

以下示例演示如何创建辅助功能感知图表控件,并使用 AccessibleObjectControl.ControlAccessibleObject 类公开可访问信息。 控件绘制两条曲线以及一个图例。 ChartControlAccessibleObject派生自 ControlAccessibleObject的 类在 方法中CreateAccessibilityInstance用于为图表控件提供自定义可访问信息。 由于图表图例不是基于实际 Control 控件的控件,而是由图表控件绘制的,因此它没有任何内置的可访问信息。 因此, ChartControlAccessibleObject 类会重写 GetChild 方法,以返回 CurveLegendAccessibleObject 表示图例每个部分的可访问信息的 。 当可访问的应用程序使用此控件时,该控件可以提供必要的可访问信息。

此代码摘录演示如何重写 Name 属性。 有关完整的代码示例, AccessibleObject 请参阅类概述。

C#
// Inner class CurveLegendAccessibleObject represents accessible information 
// associated with the CurveLegend object.
public class CurveLegendAccessibleObject : AccessibleObject
{
    private CurveLegend curveLegend;

    public CurveLegendAccessibleObject(CurveLegend curveLegend) : base() 
    {
        this.curveLegend = curveLegend;                    
    }                

    // Private property that helps get the reference to the parent ChartControl.
    private ChartControlAccessibleObject ChartControl
    {   
        get {
            return Parent as ChartControlAccessibleObject;
        }
    }

    // Internal helper function that returns the ID for this CurveLegend.
    internal int ID
    {
        get {
            for(int i = 0; i < ChartControl.GetChildCount(); i++) {
                if (ChartControl.GetChild(i) == this) {
                    return i;
                }
            }
            return -1;
        }
    }

    // Gets the Bounds for the CurveLegend. This is used by accessibility programs.
    public override Rectangle Bounds
    {
        get {                        
            // The bounds is in screen coordinates.
            Point loc = curveLegend.Location;
            return new Rectangle(curveLegend.chart.PointToScreen(loc), curveLegend.Size);
        }
    }

    // Gets or sets the Name for the CurveLegend. This is used by accessibility programs.
    public override string Name
    {
        get {
            return curveLegend.Name;
        }
        set {
            curveLegend.Name = value;                        
        }
    }

    // Gets the Curve Legend Parent's Accessible object.
    // This is used by accessibility programs.
    public override AccessibleObject Parent
    {
        get {
            return curveLegend.chart.AccessibilityObject;
        }
    }

    // Gets the role for the CurveLegend. This is used by accessibility programs.
    public override AccessibleRole Role 
    {
        get {
            return AccessibleRole.StaticText;
        }
    }

    // Gets the state based on the selection for the CurveLegend. 
    // This is used by accessibility programs.
    public override AccessibleStates State 
    {
        get {
            AccessibleStates state = AccessibleStates.Selectable;
            if (curveLegend.Selected) 
            {
                state |= AccessibleStates.Selected;
            }
            return state;
        }
    }

    // Navigates through siblings of this CurveLegend. This is used by accessibility programs.
    public override AccessibleObject Navigate(AccessibleNavigation navdir) 
    {
        // Uses the internal NavigateFromChild helper function that exists
        // on ChartControlAccessibleObject.
        return ChartControl.NavigateFromChild(this, navdir);
    }

    // Selects or unselects this CurveLegend. This is used by accessibility programs.
    public override void Select(AccessibleSelection selection) 
    {
        // Uses the internal SelectChild helper function that exists
        // on ChartControlAccessibleObject.
        ChartControl.SelectChild(this, selection);
    }
}

注解

属性 Name 是客户端用于标识、查找或报出用户的对象的字符串。 若要访问子对象的名称,必须先使用要检索其名称的子对象的索引进行调用 GetChild

继承者说明

所有对象都应支持此属性。 对象的名称应直观,以便用户了解对象的含义或用途。 此外,请确保 属性 Name 相对于父级中的任何同级对象是唯一的。 表中的导航给某些用户带来了特别困难的问题。 因此,服务器开发人员应使表单元格名称尽可能具有描述性。 例如,可以通过组合其占用的行和列的名称(例如“A1”)来创建单元格名称。但是,通常最好使用更具描述性的名称,例如“Karin,2 月”。许多对象(如图标、菜单、检查框、组合框和其他控件)都有向用户显示的标签。 向用户显示的任何标签都应用于对象的 Name 属性。 有关详细信息,请参阅 Name 属性。

如果为 Name 属性使用菜单或按钮文本,请去除标记键盘访问键的与号 (&) 。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅