我并没有实际测试以下内容,所以请自行承担使用风险。
我假设读取实际宽度/高度是线程安全的。
public FrameworkElement FindIntersectingElement(Rect rectangle, UIElement activeElement)
{
FrameworkElement found = null;
System.Threading.Tasks.Parallel.ForEach((IEnumerable<UIElement>)MainPanel.Children,
(child, loopState) =>
{
if (child != activeElement)
{
if (GetBounds(child as FrameworkElement, MainPanel).IntersectsWith(rectangle))
{
found = child as FrameworkElement;
loopState.Stop();
}
}
});
return found;
}
要回答标题问题:您可能会看到一些加速和许多嵌套元素,这可能是值得的。这(树搜索)是一个罕见的情况下,你可能会看到比线性改善更好。