[英]How to cast UIElement to Rectangle
我在 Canvas.Children 中存儲了一些矩形,在遍歷 Canvas.Children 時,我可以使用
for (int i=0; i<Canvas.Children.Count; i++)
{
UIElement ui = Canvas.Children[i];
}
但是我不知道如何將 ui 轉換為 System.Windows.Shapes.Rectangle。 有人可以幫忙嗎?
if(ui is Rectangle)
{
Rectangle rect = (Rectangle)ui;
}
這是基於之前的回答。 它可能對命中框很有用,如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.