我想知道是否有办法将其转换为更高性能 例如,使用Parallel.For。
public FrameworkElement FindIntersectingElement(Rect rectangle, UIElement activeElement)
{
foreach (var child in this.Children)
{
if (child != activeElement)
{
if (GetBounds(child as FrameworkElement, this).IntersectsWith(rectangle))
{
return child as FrameworkElement;
}
}
}
return null;
}
public Rect GetBounds(FrameworkElement of, FrameworkElement from)
{
GeneralTransform transform = null;
transform = of.TransformToVisual(from);
return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight));
}
有什么建议吗?
答案 0 :(得分:1)
我实际上没有测试以下内容,因此请自担风险( - :
我假设读取ActualWidth / Height是线程安全的。
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;
}
回答标题问题:你可能会看到一些加速和许多嵌套元素,它可能是值得的。这种(树搜索)是一种罕见的情况,您可能会看到比线性改进更好的情况。