c#的这段语句为什么执行不了,正确的格式是什么?

各位大侠好!我想自学powershell ui自动化测试,在这方面很新手。我在visio studio中编写代码,已成功执行了一个非常简单的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Automation;
using UIAutomationClientsideProviders;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var calcWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children,
            new PropertyCondition(AutomationElement.NameProperty, "计算器"));

            //按钮序列求16的平方根AutomationElement
            var buttons = calcWindow.FindAll(TreeScope.Descendants,
                new PropertyCondition(AutomationElement.ClassNameProperty, "Button"))
                .Cast<AutomationElement>()
                .Where(b => new string[] { "1", "6", "平方根" }.Contains(b.Current.Name));
            foreach (AutomationElement btn in buttons)
            {
                //获取鼠标点击模式
                var click = btn.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                //执行点击
                click.Invoke();
            }

        }
    }
}

我想再执行一段代码:

$source = @'
using System;
using System.Windows.Automation;
public static class AutomationHelper
{
public static object GetRootElement()
    {
        return System.Windows.Automation.AutomationElement.RootElement;
    }
}
'@
Add-Type -TypeDefinition $source -ReferencedAssemblies 'C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClient.dll'
[diagnostics.process]::start("notepad.exe") 
start-sleep 1
$rootElement = [AutomationHelper]::GetRootElement()
$window = new-object System.Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ControlTypeProperty, [Windows.Automation.ControlType]::Window)
$windowtext = new-object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty), "Untitled - Notepad"
$window = new-object Windows.Automation.AndCondition ($window, $windowtext) 
$windowElement = $rootElement.FindFirst("Children", $window)
# Get the menubar
$menuBar = new-object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::AutomationIdProperty), "MenuBar"
$menuBarElement = $windowElement.FindFirst("Children", $menuBar)
$itemOne = new-object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::AutomationIdProperty),"Item 1"
$itemOneElement = $menuBarElement.FindFirst("Children", $itemOne)
# Expand the File menu from the menubar 

$itemOneElement.GetCurrentPattern([System.Windows.Automation.ExpandCollapsePattern]::Pattern).Expand()
却不能成功。我看这第二段代码中没有main函数,不知这是不是不能执行成功的原因。该怎样修改呢?谢谢!

阅读 2.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏