[英]How to cast UIElement to Rectangle
I have some rectangles stored in Canvas.Children, and when traversing the Canvas.Children, I can use我在 Canvas.Children 中存储了一些矩形,在遍历 Canvas.Children 时,我可以使用
for (int i=0; i<Canvas.Children.Count; i++)
{
UIElement ui = Canvas.Children[i];
}
However I don't know how to convert ui into System.Windows.Shapes.Rectangle.但是我不知道如何将 ui 转换为 System.Windows.Shapes.Rectangle。 Could someone help?
有人可以帮忙吗?
A useful answer can be found in this related question .在这个相关问题中可以找到一个有用的答案。
Also a useful guide to casting can be found on MSDN here .也可以在 MSDN此处找到有关转换的有用指南。
Hope this helps.希望这可以帮助。
if(ui is Rectangle)
{
Rectangle rect = (Rectangle)ui;
}
This is based on earlier responses.这是基于之前的回答。 It might be useful for hit boxes, as in
collision=hitBox.IntersectsWith(rectList[rcount]);
它可能对命中框很有用,如
collision=hitBox.IntersectsWith(rectList[rcount]);
. .
Rect[] rectList;
int rcount = 0;
for (int i=0;i < CanvasB.Children.Count; i++) {
UIElement ui = CanvasB.Children[i];
if (ui is Rectangle) {
rectList[rcount] = new Rect(
Canvas.GetLeft(ui),
Canvas.GetTop(ui), (double)ui.GetValue(ActualWidthProperty),
(double)ui.GetValue(ActualHeightProperty)
);
rcount++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.