Silverlight事件处理程序响应速度极慢

时间:2012-12-30 23:13:53

标签: c# silverlight event-handling

我正在创建一个动态hexgrid,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;
    }

我做错了什么导致这种缓慢,缓慢,缓慢的反应?

1 个答案:

答案 0 :(得分:0)

在Load_Main中添加处理程序似乎有助于清除这一点。