我的应用程序中的画布上布置了一系列控件。我正在以编程方式在画布上生成一个点。我希望检测画布的子元素是否与该点相交。
我期望有一个命中测试 API 可用(wpf 曾经有一个可用的接口),但看起来所有交互似乎都是通过输入和触摸事件进行的,这使得我自己执行查询变得困难。
我很可能在搜索中错过了这个功能,有人知道如何实现这样的功能吗?
您可以使用Windows.UI.Xaml.Media.VisualTreeHelper https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.visualtreehelper.aspx.在主机坐标中查找元素 https://msdn.microsoft.com/en-us/library/windows/apps/br243040.aspx查找与特定坐标相交的 UIElements。
要找到相对于 Canvas 的点,您需要将 Canvas 的坐标转换为应用程序窗口的坐标,您可以使用 UIElement 来完成此操作。转换为视觉 https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.transformtovisual.aspx。这将考虑缩放和翻译
// myCanvas is the canvas you're hittesting on
// generatedPoint is the Point you're trying to hittest
// page is the app window's root visual
// Get a transform from the Canvas' coordinates to the Page
GeneralTransform gt = myCanvas.TransformToVisual(page);
// Use that to convert the generated Point into the page's coords
Point pagePoint = gt.TransformPoint(generatedPoint);
// and get the elements in the canvas at that point
var elements = VisualTreeHelper.FindElementsInHostCoordinates(pagePoint,myCanvas);
// elements contains the UIElements at generatedPoint (including myCanvas)
The 在主机坐标中查找元素 https://msdn.microsoft.com/en-us/library/windows/apps/br243040.aspx文档更详细地介绍了命中测试场景。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)