【问题标题】:Invoke method not working on a windows open dialog box调用方法在 Windows 打开对话框上不起作用
【发布时间】:2020-01-21 04:36:47
【问题描述】:

我尝试使用 C# 使用 Microsoft UI 自动化进行一些自动化操作,第一步我尝试使用记事本进行一些自动化操作。我试图实现的目的是启动记事本并从随机位置打开一个 txt 文件。

我已经取得的成就:

  • 打开记事本
  • 调用(展开)文件菜单并调用(单击)“打开...”按钮。
  • 将文件名的路径写入弹出打开对话框的文件名文本框中(奇怪的是,我无法在地址栏上调用单击操作,但要处理它,这将是下一个任务)。

程序.cs

using System;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Automation;
using System.Runtime.InteropServices;


namespace TestUIAutomation
{

    class Program
    {

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
    Process p = Process.Start(@"notepad.exe");
    SetForegroundWindow(p.MainWindowHandle);

    Thread.Sleep(1000);

    var baseSoftware = AutomationElement.RootElement.FindFirst(TreeScope.Children, new 
    PropertyCondition(AutomationElement.NameProperty, "Untitled - Notepad"));

    var fileButton = baseSoftware.FindFirst(TreeScope.Descendants, new 
    propertyCondition(AutomationElement.NameProperty, "File"));

    var fileButtonExpand = Utilitiy.ExCoPattern(fileButton);

    if (fileButtonExpand.Current.ExpandCollapseState != ExpandCollapseState.Expanded)
            {
                fileButtonExpand.Expand();
            }

    var openButton = baseSoftware.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Open..."));
            Utilitiy.GetInvokePattern(openButton).Invoke();

    Thread.Sleep(1000);

    var addressBar = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "File name:"));

    SendKeys.SendWait(@"C:\Users\user1\Documents\Test.txt");

    var findOpenButton = new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "Open"), new PropertyCondition(AutomationElement.AutomationIdProperty, "1"));


    var openButtonDialog = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, findOpenButton);

    Utilitiy.GetInvokePattern(openButtonDialog);
}
}
}

实用程序cs

using System;
using System.Windows.Automation;

namespace TestUIAutomation
{
    class Utilitiy
    {
        public static void SetCombobValueByUIA(AutomationElement ctrl, string newValue)
        {
            ExpandCollapsePattern exPat = ctrl.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;

            if (exPat == null)
            {
                throw new ApplicationException("Bad Control type...");
            }

            exPat.Expand();

            AutomationElement itemToSelect = ctrl.FindFirst(TreeScope.Descendants, new
                                  PropertyCondition(AutomationElement.NameProperty, newValue));

            SelectionItemPattern sPat = itemToSelect.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
            sPat.Select();            
        }
        public static InvokePattern GetInvokePattern(AutomationElement element)
        {
            return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        }
        public static TogglePattern CheckBoxPattern(AutomationElement element)
        {
            return element.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
        }
        public static SelectionItemPattern RadioButtonPattern(AutomationElement element)
        {
            return element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
        }
        public static ValuePattern TextValuePattern(AutomationElement element)
        {
            return element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
        }
        public static ExpandCollapsePattern ExCoPattern(AutomationElement element)
        {
            return element.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
        }
    }
}

目前的问题我不会收到任何错误消息,所以程序实际上调用了打开按钮,但在现实生活中什么也没有发生。 我可以使用 sendkeys 方法输入输入来打开文件,但如果可能的话,我喜欢调用打开按钮。

【问题讨论】:

    标签: c# windows modal-dialog ui-automation


    【解决方案1】:

    好的,我找到了解决方案,我忘了实际调用按钮。

    我从这里更改了最后一行

    Utilitiy.GetInvokePattern(openButtonDialog);
    

    到这里

    Utilitiy.GetInvokePattern(openButtonDialog).Invoke();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-04
      • 2019-08-20
      • 2014-11-18
      • 1970-01-01
      • 2014-04-13
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多