使用枚举参数GetPattern()实现WPF UI Automation system的方法PatternInterface,我们通常以以下方式使用它:
//Code with original implementation
ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(button1);
IInvokeProvider provider = (IInvokeProvider)buttonPeer.GetPattern(PatternInterface.Invoke); //Line in Question
//To invoke the click event of button we then use the following code:
provider.Invoke();从上面的代码来看,问题中的注释行似乎不是强类型的,我们需要将从GetPattern()方法返回到所需的接口,然后使用它来调用特定的UI自动化。
的问题是:如果在WPF中使用已经存在的Generics在.Net框架中实现GetPattern()方法,情况就不会更好,如下所示:
public T GetPattern<T>;GetPattern<T>方法时传递所需的接口模式名称,并获得该接口实例的强类型,并且not还需要cast。微软在GetPattern()方法的最初实现中给出了什么想法?GetPattern()原始实现的可维护性。当需要支持新的控制接口模式时,需要将该模式接口的枚举值添加到名为PatternInterface的enum参数中。我认为调用该方法并使用下面使用调用泛型实现的新代码获取接口模式会更容易和更好:
//Code with New Generics based implementation
ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(button1);
IInvokeProvider provider = buttonPeer.GetPattern<IInvokeProvider>(); //Line in Question
//To invoke the click event of button we then use the following code:
provider.Invoke();发布于 2012-06-17 17:47:02
这是因为通常的原因:他们没有时间机器。从参考源代码中可用的源代码文件中的“历史”注释中可以看到,UI自动化类的工作在2003年6月左右开始,有证据表明它是从以前的工作中派生出来的。直到2005年才有仿制药上市。
来自dd/wpf/src/UIAutomation/UIAutomationTypes/System/Windows/AutomationPattern.cs:
// History:
// 06/02/2003 : BrendanM Ported to WCP很有可能是布兰登McKeon。对于"WCP“可能意味着什么,没有一个像样的猜测。
https://stackoverflow.com/questions/11073151
复制相似问题