我正在创建一个动态六边形,8列10个六边形,椭圆以随机六边形为中心。
我最初认为事件处理程序没有加载。现在看来,他们正在加载,但反应非常缓慢。一两分钟后,当我将鼠标移到生成的网格上时,十六进制开始随机填充,工具提示开始显示(但直到该十六进制的事件处理程序响应之后)。实际上,最后一个十六进制需要几分钟才能响应mouseover事件。
private void CreateHexGrid(int columns, int rows, double length)
{
//I create a jagged array of points,
//hexpoint[# of columns, # of rows, 6 points]
//the base (0,0) hex is generated point by point
//mathematically based on the length of one side
//then each subsequent hex is mathematically
//created based on the points of the previous hex.
//Finally, I instantiate each Polygon hex and fill
//its points collection using the array.
PointCollection points = new PointCollection();
SolidColorBrush blackBrush = new SolidColorBrush(Colors.Black);
SolidColorBrush clearBrush = new SolidColorBrush(Colors.Transparent);
Polygon hex = new Polygon();
hex.Stroke = blackBrush;
hex.StrokeThickness = 1;
hex.Name = "Hex" + column.ToString() + row.ToString();
for (int point = 0; point < 6; point++)
{
points.Add(HexPoint[column, row, point]);
}
hex.Points = points;
ToolTipService.SetToolTip(hex, hex.Name);
hex.MouseEnter += new MouseEventHandler(hex_MouseEnter);
cnvsHexGrid.Children.Add(hex);
//....
}
目前我有处理程序只是试图改变多边形/椭圆的填充颜色来测试。我最终想生成一个交互式弹出窗口,如果十六进制被点击。
void hex_MouseEnter(object sender, MouseEventArgs e)
{
var hex = sender as Polygon;
SolidColorBrush blueFill = new SolidColorBrush(Colors.Blue);
hex.Fill = blueFill;
}
我做错了什么,引起这么慢,慢,慢的反应?
在Load_Main中添加一个处理程序似乎有助于解决这个问题。