Silverlight开发历程—(用C#来绘制图形)

 在Silverlight中利用C#来绘制图形比较简单,经常用的两种方法是直接创建对象然后添加到页面容器中和创建XAML创建对象然后利用XamlReader.Load方法加载到容器中。

比较简单,直接上代码:

第一种:

public void DrawPolyLine()
        {
            //创建Polyline
            Polyline 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()
        {
            //创建Polygon
            Polygon 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 = "<Path ";
            //引用
            xaml+=" xmlns=\"http://schemas.microsoft.com/client/2007\"";
            //创建属性
            xaml += " Stroke=\"Blue\" StrokeThickness=\"3\" ";
            xaml += string.Format(" Data=\"{0}\" />", "M 10,100 Q100,200 300,100");
            //创建路径对象
            Path path = new Path();

            path = (Path)XamlReader.Load(xaml);

            LayoutRoot.Children.Add(path);
        }


运行结果:

 

下一节将综合前台学到的绘图方式来绘制一个报表折线统计图。

posted @ 2011-11-08 17:31  Bodi  阅读(288)  评论(0编辑  收藏  举报