网格线导致WPF中的性能问题

时间:2019-04-30 12:19:18

标签: c# wpf xaml

我正在WPF / XAML中创建一个钢琴卷应用程序,在其中我想要一个网格,其中的线具有不同的粗细,例如:

Piano Roll

每当我需要在音乐中添加新的小节时,这些行就会动态添加。它们不是边界线。每个对象都是其自己的Line对象,添加到覆盖钢琴卷的画布上。我使用以下循环添加它们:

for (int j = 1; j <= 32; j++)
{
    // Drawing the vertical grid lines with varying thickness.
    if (j % 32 == 0) thickness = 2.3;
    else if (j % 16 == 0) thickness = 1.1;
    else if (j % 8 == 0) thickness = 0.9;
    else if (j % 4 == 0) thickness = 0.6;
    else if (j % 2 == 0) thickness = 0.4;
    else thickness = 0.2;

    Line gridLine = new Line
    {
        Stroke = (Brush)this.Resources["GridLineColour"],
        X1 = 0,
        X2 = 0,
        Y1 = 0,
        Y2 = height,
        HorizontalAlignment = HorizontalAlignment.Left,
        VerticalAlignment = VerticalAlignment.Center,
        StrokeThickness = thickness
    };

    Panel.SetZIndex(gridLine, 2);
    Canvas.SetLeft(gridLine, j * thirtySecondNote + barStart);
    Canvas.SetTop(gridLine, 7);

    gridLines.Add(gridLine);
    cnvGridLines.Children.Add(gridLine);
}

这确实对我不起作用,因为当我缩小并且屏幕上有很多行时,当我尝试滚动时,应用程序的运行速度大大降低。我确定这是问题所在,因为如果将网格线的可见性设置为“隐藏”,则程序运行正常。

我是WPF的新手,我想不出一种解决方案,这些解决方案可以满足我的要求,并且不会减慢一切。我的要求是:

  • 每条线的厚度不同。
  • 可调位置。
  • 能够通过代码动态添加音乐线条。

有帮助吗?

0 个答案:

没有答案