我正在使用Microsoft Visual Studio 2010 Express for Windows Phone进行编码。我需要在Canvas
上添加一个点,但我不能......
for (float x = x1; x < x2; x += dx)
{
Point poin = new Point();
poin.X = x;
poin.Y = Math.Sin(x);
canvas1.Children.Add(poin);
}
工作室说:
错误2参数1:无法从'System.Windows.Point'转换为'System.Windows.UIElement'
我的问题是:如何在Canvas
上添加一个点?
答案 0 :(得分:2)
从您的代码段我假设您正在尝试绘制曲线。为此,您可以查看GraphicsPath
。您可以使用点作为坐标,而不是绘制单个点。然后,在您的代码中,您可以使用GraphicsPath
方法创建AddLine
。
然后可以将其绘制到位图上,例如。
编辑
样品(未测试):
GraphicsPath p = new GraphicsPath();
for (float x = x1; x < x2; x += dx)
{
Point point = new Point();
point.X = x;
point.Y = Math.Sin(x);
Point point2 = new Point();
point2.X = x+dx;
point2.Y = Math.Sin(x+dx);
p.AddLine(point, point2);
}
graphics.DrawPath(p);
另一种方法是使用WPF Path
类,它可以使用相同的类,但是可以添加到Canvas
的子元素的真实UI元素。
编辑
人们已经指出上面的代码是Windows Forms代码。好吧,这是你在WPF中可以做的事情:
myPolygon = new Polygon();
myPolygon.Stroke = System.Windows.Media.Brushes.Black;
myPolygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
myPolygon.StrokeThickness = 2;
myPolygon.HorizontalAlignment = HorizontalAlignment.Left;
myPolygon.VerticalAlignment = VerticalAlignment.Center;
PointCollection points = new PointCollection();
for (float x = x1; x < x2; x += dx)
{
Point p = new Point(x, Math.Sin(x));
points.Add(p);
}
myPolygon.Points = points;
canvas1.Children.Add(myPolygon);
答案 1 :(得分:1)
您使用的Point
不是UIElement
而是结构,请改用Line
。
Line lne = new Line();
lne.X1 = 10;
lne.X2 = 11;
lne.Y1 = 10;
lne.Y2 = 10;
canvas1.Children.Add(lne);
你明白了......
修改强>
改变:
lne.X2 = 10到lne.X2 = 11
答案 2 :(得分:1)
如果它只是你要添加的单个点,你可以在画布上添加一个小矩形或椭圆。
如果您想多次设置很多积分或几个积分,我建议您create an array of pixel data (colors)并将其写入WriteableBitmap
答案 3 :(得分:0)
根据错误,Canvas
控件的子项必须是System.Windows.UIElement
类的派生词:System.Windows.Point
不是。{{1}}。为了实现您的目标,您最好在WPF中使用几何体。有关如何操作的文章,请参阅here。
答案 4 :(得分:0)
尝试添加椭圆
Ellipse myEllipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.White;
myEllipse.Width = 200;
myEllipse.Height = 100;
Canvas.SetTop(myEllipse,50);
Canvas.SetLeft(myEllipse,80);
myCanvas.Children.Add(myEllipse);