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);
        }


运行结果:

 

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

转载于:https://www.cnblogs.com/raphael5200/archive/2011/11/08/5114896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值