2
我想使用Path繪製一些線條。運行程序時,它總是顯示錯誤。錯誤發生在這句話:「myPathSegmentCollection.Add(myLineSegment [i]);」 解決方案可以建立。調試時,顯示「元素已經是另一個元素的孩子」在windows phone 7中畫線?
下面是我的函數:
public void drawline(Point endP)
{
PathFigenter code hereure myPathFigure = new PathFigure();
myPathFigure.StartPoint = endP;
LineSegment [] myLineSegment = new LineSegment[5];
Point myPoint = new Point();
LineSegment line = new LineSegment();
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
for (int i = 0; i < 5; i++)
{
myPoint.X = i + 10.0;
myPoint.Y = i+1.0;
line.Point = myPoint;
myLineSegment[i] = line;
myPathSegmentCollection.Add(myLineSegment[i]);
}
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
}
是否有任何人能幫助嗎?謝謝!
是的,沒錯。非常感謝你。 –
另一個問題。實際上,我已經爲函數上面的myLineSegment []指定了內存,例如「LineSegment [] myLineSegment = new LineSegment [5]」,我認爲每個元素都有獨立的內存。是什麼原因? –
@Xiao_ga:'LineSegment [] myLineSegment = new LineSegment [5]'只保留5個指向LineSegment類型的對象的指針,而不是實際的對象本身。 C#數組都是關於單獨的對象引用,而不是像C/C++這樣的內存數組 –