多条线绘制导致 'Specified Visual is already a child of another Visual' 错误

Multiple line draws cause 'Specified Visual is already a child of another Visual' error

我正在循环绘制一系列线条(省略不重要的内容):

     for (int i = 0; i < TLS.NumEdges - 10; i++)
            {
                MSTLine.X1 = BlueArmy[TLS.EdgeList[i, 0]].Location.X;
                MSTLine.Y1 = BlueArmy[TLS.EdgeList[i, 1]].Location.Y;   
                MSTLine.X2 = BlueArmy[TLS.EdgeList[i + 1, 0]].Location.X;
                MSTLine.Y2 = BlueArmy[TLS.EdgeList[i + 1, 0]].Location.Y;
                ((MainWindow)System.Windows.Application.Current.MainWindow).AICanvas.Children.Add(MSTLine);
            }

它抛出这个运行时错误: System.ArgumentException: 'Specified Visual is already a child of another Visual or the root of a CompositionTarget.'

我假设它不喜欢多次 Children.Add 调用。那么从循环内部绘制一系列线条的正确方法是什么?由于其他原因,我宁愿不使用折线。

GUI 元素是 类,不是结构。您必须为要创建的每条线创建一个新的 MSTLine 实例。