我正在使用Microsoft.UI.Composition
WinUI 3 中的命名空间将视觉效果渲染到Canvas
如下所示:
internal Compositor _compositor; // Compositor of Canvas control
internal ContainerVisual _root; // Container of Canvas control
var roundedRectangle = _compositor.CreateRoundedRectangleGeometry();
roundedRectangle.Size = new Vector2(200, 150);
roundedRectangle.CornerRadius = new Vector2(10, 10);
var spriteShape = _compositor.CreateSpriteShape(roundedRectangle);
spriteShape.FillBrush = _compositor.CreateColorBrush(Colors.Transparent);
spriteShape.StrokeBrush = _compositor.CreateColorBrush((Color)Application.Current.Resources["SystemAccentColor"]);
spriteShape.StrokeThickness = 2;
spriteShape.Offset = new Vector2(2, 2);
var shapeVisual = StmVisual._compositor.CreateShapeVisual();
shapeVisual.Size = new Vector2(204, 154);
shapeVisual.Offset = new Vector3(10, 10, 0);
shapeVisual.Shapes.Add(spriteShape);
shapeVisual.IsHitTestVisible = true;
_root.Children.InsertAtTop(shapeVisual);
但是,当我尝试Canvas
使用VisualTreeHelper.FindElementsInHostCoordinates进行测试时,它不会产生任何结果,因为它是 aShapeVisual
而不是UIElement
。怎么可能命中测试ShapeVisual
?我需要深入CanvasGeometry.FillContainsPoint()
研究 Win2D 还是有更简单的方法?