Silverlight开发历程—(用C#来绘制图形)
在Silverlight中利用C#来绘制图形比较简单,经常用的两种方法是直接创建对象然后添加到页面容器中和创建XAML创建对象然后利用XamlReader.Load方法加载到容器中。
比较简单,直接上代码:
第一种:
public void DrawPolyLine(){//创建PolylinePolyline polyline = new Polyline();//创建坐标集合PointCollection points = new PointCollection();points.Add(new Point(50,300));points.Add(new Point(50,50));points.Add(new Point(200,300));polyline.Points = points;//填充背景色与线条颜色polyline.Fill = new SolidColorBrush(Colors.Orange);polyline.Stroke = new SolidColorBrush(Colors.Black);polyline.StrokeThickness = 3;//设置位置Canvas.SetTop(polyline, 50);Canvas.SetLeft(polyline, 50);//向容器添加控件LayoutRoot.Children.Add(polyline);}public void DrawPolygon(){//创建PolygonPolygon polygon = new Polygon();//创建坐标集合PointCollection points = new PointCollection();points.Add(new Point(50, 300));points.Add(new Point(50, 50));points.Add(new Point(200, 300));polygon.Points = points;//填充背景色与线条颜色polygon.Fill = new SolidColorBrush(Colors.Orange);polygon.Stroke = new SolidColorBrush(Colors.Black);polygon.StrokeThickness = 3;//设置位置Canvas.SetTop(polygon, 50);Canvas.SetLeft(polygon, 300);//向容器添加控件LayoutRoot.Children.Add(polygon);}
运行结果:

第二种:
public void DrawPathWithXML(){string xaml = " ", "M 10,100 Q100,200 300,100");//创建路径对象Path path = new Path();path = (Path)XamlReader.Load(xaml);LayoutRoot.Children.Add(path);}
运行结果:

下一节将综合前台学到的绘图方式来绘制一个报表折线统计图。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
