我有2种方法。一个动态创建图像,另一个动态删除图像。第一种方法一切正常。在第二种方法中,如果我指定要删除图像的位置(RemoveAt()),它也会起作用。我知道该图像是用ID 13创建的。是否有任何方法可以动态获取var m的ID?谢谢。
private static void CreateImage ( double imageWidth, double imageHeight, double xPos, double yPos, string imageSource, string imageName)
{
m = new Image();
mainWindow main = Application.Current.Windows[0] as mainWindow;
main.mainGrid.Children.Add(m);
m.Height = imageHeight; //Image height
m.Width = imageWidth;//Image width
//Width conversion to X
double halfWidth = main.Width;
double w = ( -halfWidth + xPos + imageWidth ) ;
Convert.ToInt32 ( Math.Round(w) );
//Height conversion to Y
double halfHeight = main.Height;
halfHeight = -halfHeight;
double h = ( halfHeight + yPos + imageHeight ) ;
Convert.ToInt32 ( Math.Round(h) );
m.Margin = new Thickness(w, h, 0, 0);//left, top, right, bottom
m.Source = new BitmapImage(new Uri(@"Resources\" + imageSource + ".png", UriKind.Relative));
m.Name = imageName;
}
private static void DestroyImage()
{
mainWindow main = Application.Current.Windows[0] as mainWindow;
main.mainGrid.Children.RemoveAt(m.);
}