在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)); 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); }
运行结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。