【发布时间】:2015-10-31 15:44:44
【问题描述】:
我创建了一个创建六边形多边形并将其作为对象检索的方法,但我不知道如何使用所述对象。代码如下:
public object Hexagon()
{
float h;
float r;
int x = 20;
int y = 20;
int s = 10;
h = HexOperations.ConvertToFloat(HexOperations.Calc(s));
r = HexOperations.ConvertToFloat(HexOperations.Calc(s));
Point[] vert= new Point[6];
vert[0] = new Point(x, y);
vert[1] = new Point(x + s, y);
vert[2] = new Point(x + s + h, y + r);
vert[3] = new Point(x + s, y + r + r);
vert[4] = new Point(x, y + r + r);
vert[5] = new Point(x - h, y + r);
Polygon pol = new Polygon();
System.Windows.Media.PointCollection pointC = new System.Windows.Media.PointCollection();
pointC.Add(vert[0]);
pointC.Add(vert[1]);
pointC.Add(vert[2]);
pointC.Add(vert[3]);
pointC.Add(vert[4]);
pointC.Add(vert[5]);
pol.Points = pointC;
pol.Stroke = Brushes.Black;
return pol;
}
如果我在返回之前添加“MainGrid.Children.Add(pol)”,我可以看到网格“MainGrid”上打印的六边形,但我只是不知道如何在上述方法之外使用它。我试过这个:
MainGrid.Children.Add(Hexagon());
这给了我错误“无法从‘object’转换为 System.Window.UIElement”。
也试过了:
Polygon poly = new Hexagon();
其中说“新表达式需要 ()。[], {}, ;, 在类型之后”。
还有:
Hexagon poly = new Hexagon();
这显然给了我指点。我只是不知道还能尝试什么。可能是因为我的方法犯了一个基本错误,但无论如何,提前谢谢你。
【问题讨论】:
-
Hexagon()是一个函数,而不是一个对象。不宜使用new。Polygon poly = Hexagon();应该可以正常工作。