我们使用 AutomationUIClient 控制台应用程序来测试我们的 WPF 应用程序。
我们在 WPF 应用程序中使用 ISelectionProvider 和 IExpandCollapseProvider 设置了一个自定义类 TreeItemAutomationPeer
如果自动化应用程序控制台使用此对象,则保留所有者(在我的情况下为 TreeItem),因此它会泄漏...
我们添加了最后一个方法 GetChildrenCore() 来防止 Children 的内存泄漏。
public class TreeItemAutomationPeer : FrameworkElementAutomationPeer, ISelectionItemProvider, IExpandCollapseProvider
{
private readonly TreeItem _treeItem;
public TreeItemAutomationPeer(TreeItem treeItem)
: base(treeItem)
{
_treeItem = treeItem;
}
public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.SelectionItem ||
patternInterface == PatternInterface.ExpandCollapse)
return this;
return base.GetPattern(patternInterface);
}
protected override string GetClassNameCore()
{
return "TreeItem";
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
//return AutomationControlType.Tree;
return AutomationControlType.Custom;
}
#region ISelectionItemProvider
public IRawElementProviderSimple SelectionContainer
{
get { return _treeItem.SelectionContainer; }
}
public bool IsSelected { get { return _treeItem.Item.IsSelected; } }
public void AddToSelection()
{
_treeItem.Item.IsSelected = true;
ItemHelper.SelectItem(_treeItem, _treeItem.Item);
}
public void RemoveFromSelection()
{
_treeItem.Item.IsSelected = false;
}
public void Select()
{
if (_treeItem.Item.IsSelected)
RemoveFromSelection();
else AddToSelection();
}
#endregion
#region IExpandCollapseProvider
public ExpandCollapseState ExpandCollapseState
{
get
{
return _treeItem.Item.IsExpanded
? ExpandCollapseState.Expanded
: ExpandCollapseState.Collapsed;
}
}
相关分类