WPF开发ArcGIS时候,Graphic模板中不能使用ViewBox控件的原因说明
今天在项目中遇到一个问题,触摸屏点击图层上的Graphic的时候使用GraphicsLayer.FindGraphicsInHostCoordinates(Point)方法不能取到对应的Graphic,经过和其他能点击到的模板对比发现改模板是放在一个ViewBox里面(大概原来编写代码的同学是想要Graphic随着分辨率大小变化吧,我们的程序要适配2k和4k两种屏幕),感到不解,所以反编译了源代码看了一下:
public IEnumerable FindGraphicsInHostCoordinates(Point intersectingPoint)
{
if (base.Canvas.Parent == null)
{
return new Graphic[0];
}
if (!this.IsHitTestVisible)
{
return new Graphic[0];
}
Visual rootVisual = ESRI.ArcGIS.Client.Utils.Transform.GetRootVisual(base.Map);
if (rootVisual == null)
{
return new Graphic[0];
}
intersectingPoint = rootVisual.TransformToVisual(base.Canvas).Transform(intersectingPoint);
HitTestResult hitTestResult = VisualTreeHelper.HitTest(base.Canvas, intersectingPoint);
if (hitTestResult == null || !(hitTestResult.VisualHit is UIElement))
{
return new Graphic[0];
}
return this.GetGraphicsFromUIElements(new UIElement[]
{
(UIElement)hitTestResult.VisualHit
});
}
private IEnumerable GetGraphicsFromUIElements(IEnumerable elements)
{
List list = new List();
foreach (UIElement current in elements)
{
UIElement uIElement = current;
if (uIElement.Visibility != Visibility.Collapsed && uIElement.IsHitTestVisible)
{
if (!(current is GraphicElement))
{
while (uIElement != null && !(uIElement is GraphicElement) && uIElement.Visibility == Visibility.Visible)
{
uIElement = (VisualTreeHelper.GetParent(uIElement) as UIElement);
}
}
if (uIElement is GraphicElement)
{
GraphicElement graphicElement = uIElement as GraphicElement;
if (graphicElement.Graphic != null && graphicElement.Visibility == Visibility.Visible)
{
GraphicCollection graphicCollection = graphicElement.Graphic.GetValue(Clusterer.ClusterProperty) as GraphicCollection;
if (graphicCollection != null)
{
foreach (Graphic current2 in graphicCollection)
{
if (!list.Contains(current2))
{
list.Add(current2);
yield return current2;
}
}
}
else if (!list.Contains(graphicElement.Graphic))
{
list.Add(graphicElement.Graphic);
yield return graphicElement.Graphic;
}
}
}
}
}
list.Clear();
list = null;
yield break;
}