小编NCa*_*ell的帖子

使用UI自动化以编程方式打开上下文菜单

我正在尝试使用UI自动化实现右键单击上下文菜单.由于UI自动化没有本机右键单击模式,因此我将ExpandCollapse提供程序添加到listview的AutomationPeer类,并将展开和折叠映射到打开和关闭上下文菜单.

我的问题是,有没有更好的方法来调用上下文菜单,而不涉及尝试使用私有构造函数实例化一个类?我不能使用Shift-F10的SendKeys.我想使用PopupControlService,但标记为内部.

我糟糕的解决方法:

public class MyListViewAutomationPeer : ListViewAutomationPeer, IExpandCollapseProvider
{

    public MyListViewAutomationPeer(MyListView owner)
        : base(owner){}

    public override object GetPattern(PatternInterface patternInterface)
    {
        if (patternInterface == PatternInterface.ExpandCollapse)
        {
            return this;
        }
        return base.GetPattern(patternInterface);
    }

    public void Expand()
    {
        MyListView owner = (MyListView)Owner;

        //**********************
        //Ouch!!! What a hack
        //**********************

        //ContextMenuEventArgs is a sealed class, with private constructors
        //Instantiate it anyway ...
        ContextMenuEventArgs cmea = (ContextMenuEventArgs)FormatterServices.GetUninitializedObject(typeof(ContextMenuEventArgs));
        cmea.RoutedEvent = MyListView.ContextMenuOpeningEvent;
        cmea.Source = owner;

        //This will fire any developer code that is bound to the OpenContextMenuEvent …
Run Code Online (Sandbox Code Playgroud)

c# automation contextmenu routedevent

5
推荐指数
1
解决办法
3345
查看次数

标签 统计

automation ×1

c# ×1

contextmenu ×1

routedevent ×1