调用方法不适用于 windows 打开的对话框

Invoke method not working on a windows open dialog box

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

我已经取得的成就:

Program.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);
}
}
}

实用程序

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 方法输入输入来打开文件,但如果可能的话,我喜欢调用打开按钮。

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

我更改了最后一行

Utilitiy.GetInvokePattern(openButtonDialog);

对此

Utilitiy.GetInvokePattern(openButtonDialog).Invoke();