我目前正在尝试WPF,因为我以前使用过WinForms。我从一个小练习开始,想要创建一个程序,在你点击画布的地方划一条线。我以编程方式将该行添加到画布中,但似乎它没有绘制它。这些线条成为画布的孩子们。
C#代码
DoEvents
XAML
private ArrayList arl = new ArrayList();
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
drawArea.Children.Clear();
arl.Add(Mouse.GetPosition(drawArea));
Point[] points = new Point[arl.Count];
for (int i = 0; i < arl.Count; i++)
points[i] = (Point)arl[i];
Line[] lines = new Line[points.Length - 1];
if(lines.Count() > 0)
{
for (int i = 0; i < lines.Length; i++)
{
lines[i] = new Line();
lines[i].Stroke = SystemColors.WindowFrameBrush;
lines[i].X1 = points[i + 1].X;
lines[i].X2 = points[i].X;
lines[i].Y1 = points[i + 1].Y;
lines[i].Y2 = points[i].Y;
}
foreach (var item in lines)
drawArea.Children.Add(item);
}
}