如何解决N 单元-无法在窗体中使用按钮名称访问工具条按钮
我正在尝试使用 Windows 窗体中的按钮名称访问 Windows 窗体中的工具条按钮,但最终结果面临空引用异常。通常我可以使用它的名字来调用按钮,但是我在调用工具条按钮 2 中的按钮时遇到了问题。在添加工具条 1 之后,自动化元素识别到工具条 1 无法识别工具条按钮 2 这个问题发生在 N 单元框架和使用访问按钮自动化元素。我可以调用工具条按钮 1,但不能调用工具条按钮 2。有人可以指导我解决这个问题吗?
访问按钮的代码片段:
public void ClickButton()
{
object objPattern;
AutomationElement click = AutomationElement.FindFirst(TreeScope.Descendants,new PropertyCondition(AutomationElement.NameProperty,"toolStripButton2"));
click.TryGetCurrentPattern(InvokePattern.Pattern,out objPattern);
((InvokePattern)objPattern).Invoke();
}
工具条的代码片段:
enter code here
// toolStrip1
//
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1,this.toolStripComboBox1,this.toolStripButton2});
this.toolStrip1.Location = new System.Drawing.Point(0,0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Text = "toolStripButton1";
//
// toolStripComboBox1
//
this.toolStripComboBox1.Name = "toolStripComboBox1";
this.toolStripComboBox1.Size = new System.Drawing.Size(121,25);
//
// toolStripButton2
//
this.toolStripButton2.Name = "toolStripButton2";
this.toolStripButton2.Text = "toolStripButton2";
//
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton1;
private System.Windows.Forms.ToolStripComboBox toolStripComboBox1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。