在uwp ui系统中,使用shape是绘制2d图形最简单的方式,小到图标,大到图表都用到shape的派生类,可以说有举足轻重的地位。幸运的是从silverlight以来shape基本没有什么大改动,简直是uwp中的一股清流。
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477011351.jpg)
上图来自Pro Silverlight 5 in C#,可见Silverlight中的Shape和UWP的Shape基本架构一致。Shape的API从WPF以来就几乎没变,对熟悉WPF/Silverlight的开发者来说几乎是零学习成本。
1. Ellipse(圆形)Ellipse没有在Shape的基础上增加任何属性,是Shape的派生类中最简单的一个。
1.1 Fill、Stroke与StrokeThicknesspublic Brush Fill { get; set; }public Brush Stroke { get; set; }public System.Double StrokeThickness { get; set; }<pre class="brush:php;toolbar:false;"><Ellipse Height="100" Width="100" Fill="#FF7E9EC0" Stroke="#FFFF0EC4" StrokeThickness="5" Margin="10" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477027515.jpg)
public Stretch Stretch { get; set; }效果如下
代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><StackPanel Orientation="Horizontal"> <Ellipse Fill="#FF7E9EC0" Stroke="#FFFF0EC4" StrokeThickness="5" Height="50" Width="100" Stretch="Fill" /> <Ellipse Fill="#FF7E9EC0" Stroke="#FFFF0EC4" StrokeThickness="5" Height="50" Width="100" Stretch="None" /> <Ellipse Fill="#FF7E9EC0" Stroke="#FFFF0EC4" StrokeThickness="5" Height="50" Width="100" Stretch="Uniform" /> <Ellipse Fill="#FF7E9EC0" Stroke="#FFFF0EC4" StrokeThickness="5" Height="50" Width="100" Stretch="UniformToFill" /></StackPanel>
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477081592.jpg)
public DoubleCollection StrokeDashArray { get; set; }用于将边框变成虚线。StrokeDashArray的值是一个double类型的有序集合,集合中的值指虚线中每一段的长度,长度单位是边框值的宽度。例如以下圆形:
代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><Ellipse StrokeDashArray="1,2,3" Stroke="#FFFF0EC4" StrokeThickness="10" Height="200" Width="200" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477129849.jpg)
边框宽度为10,虚线的第一段是长度为10的实线,第二段为长度为20的空白,第三段为长度为30的实线,然后如此循环直到结束。
自从开始WPF工作以来,我一直将StrokeDashArray的值设为一个好看又好记的值:4 2,the answer to life, the universe, and everything。
代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><Ellipse StrokeDashArray="4 2" Stroke="#FFFF0EC4" StrokeThickness="3" Height="200" Width="200" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477150288.jpg)
public PenLineCap StrokeDashCap { get; set; }<pre class="brush:php;toolbar:false;"><Grid Margin="10,10" Width="200" Height="10" Background="Gray"> <Line Stretch="Fill" Stroke="Red" StrokeThickness="10.5" X2="1" StrokeEndLineCap="Flat" /></Grid><TextBlock Text="Flat" Grid.Column="1" VerticalAlignment="Center" /><Grid Width="200" Margin="10,10" Height="10" Background="Gray" Grid.Row="1"> <Line Stretch="Fill" Stroke="Red" StrokeThickness="10" X2="1" StrokeEndLineCap="Round" /></Grid><TextBlock Text="Round" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" /><Grid Width="200" Margin="10,10" Height="10" Background="Gray" Grid.Row="2"> <Line Stretch="Fill" Stroke="Red" StrokeThickness="10" X2="1" StrokeEndLineCap="Square" /></Grid><TextBlock Text="Square" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" /><Grid Width="200" Margin="10,10" Height="10" Background="Gray" Grid.Row="3"> <Line Stretch="Fill" Stroke="Red" StrokeThickness="10" X2="1" StrokeEndLineCap="Triangle" /></Grid><TextBlock Text="Triangle" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477161983.jpg)
public System.Double StrokeDashOffset { get; set; }用于指定虚线开始的位置。
2. Rectangle(矩形)Rectangle 只比Shape多了RadiusX和RadiusY两个属性。
2.1 RadiusX和RadiusYpublic System.Double RadiusX { get; set; }和public System.Double RadiusY { get; set; }分别用于指定用于使矩形的角变圆的椭圆的x轴和 y轴半径。
如下面这个例子,可以看到
RadiusX="50" RadiusY="20"
Width="100" Height="40"
<pre class="brush:php;toolbar:false;"><Rectangle Height="100" Width="100" Fill="#FF7E9EC0" Stroke="#FFFF0EC4" StrokeThickness="5" RadiusX="50" RadiusY="20" /><Ellipse HorizontalAlignment="Left" VerticalAlignment="Top" StrokeThickness="5" Stroke="Yellow" Fill="Red" Width="100" Height="40" Opacity="0.5" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477128983.jpg)
public PenLineJoin StrokeLineJoin { get; set; }public System.Double StrokeMiterLimit { get; set; }![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477188734.jpg)
Line表示从第一个点(X1,Y1)到第二个点(X2,Y2)的一条直线。
3.1 X1,Y1,X2,Y2这四个属性确定了Line的起点和终点。
除了使用绝对值定位Line的位置,还可以使用相对定位。
代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><Line Stretch="Fill" X1="0" X2="1" StrokeDashArray="4 2" Stroke="BlueViolet" StrokeThickness="1" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477176127.jpg)
public PenLineCap StrokeStartLineCap { get; set; }public PenLineCap StrokeEndLineCap { get; set; }
WeMuseum博物馆微门户小程序主要功能包括展厅展馆、当前展览、馆藏文物、服务指南、活动讲座预约、参观预约等。具有“即开即用,用完即走”特点的微信小程序,创新了博物馆行业的服务方式,是传播博物馆服务和文化的便捷高效途径之一。微信小程序在博物馆中的应用,将会为博物馆文化的传播开辟出新渠道、新路径。我们的思路就是:轻量化前端,深度开发后台。
0
这两个形状具有相同的属性,外观也相似。区别只是如果Points的最后一个点和第一个点不一样,Polygon会自动将这两个点连接到一起。
4.1 Pointspublic PointCollection Points { get; set; }<pre class="brush:php;toolbar:false;"><Polygon Points="15,200 68,70 110,200 0,125 135,125" Fill="Red" Stroke="Blue" StrokeThickness="4" Stretch="Fill" Height="100" Width="100" /><Polyline Points="15,200 68,70 110,200 0,125 135,125" Fill="Red" Stroke="Blue" StrokeThickness="4" Stretch="Fill" Height="100" Width="100" Margin="20,0,0,0" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477148360.jpg)
public FillRule FillRule { get; set; }![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477264217.jpg)
Path是功能最强大的形状,它基本上由上面的其它形状组成并且可以替代它们中的任何一个。只是Path使用起来也比较复杂,只有在图表和少量场景用到,很多时候比起构造复杂的Path还不如用直接用图片。
5.1 Datapublic Geometry Data { get; set; }除此之外,还可以使用由多个Geometry组成的GeometryGroup构造组合形状。
5.2 PathGeometry在所有Geometry中PathGeometry是功能最强大的,唯一的缺点是太多复杂。它使用
public PathFigureCollection Figures { get; set; }下面是一个Path的官方示例,同时使用了各种Geometry及部分PathSegment:
代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><Path Stroke="Black" Margin="10" StrokeThickness="1" Fill="#CCCCFF"> <Path.Data> <GeometryGroup> <RectangleGeometry Rect="50,5 100,10" /> <RectangleGeometry Rect="5,5 95,180" /> <EllipseGeometry Center="100, 100" RadiusX="20" RadiusY="30" /> <RectangleGeometry Rect="50,175 100,10" /> <PathGeometry> <PathGeometry.Figures> <PathFigureCollection> <PathFigure IsClosed="true" StartPoint="50,50"> <PathFigure.Segments> <PathSegmentCollection> <BezierSegment Point1="75,300" Point2="125,100" Point3="150,50" /> <BezierSegment Point1="125,300" Point2="75,100" Point3="50,50" /> </PathSegmentCollection> </PathFigure.Segments> </PathFigure> </PathFigureCollection> </PathGeometry.Figures> </PathGeometry> </GeometryGroup> </Path.Data></Path>
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477293284.jpg)
Path的XAML虽然具备很高的可读性,但在存储上不具优势。所以UWP提供了一种替代语法:图形微语言。图形微语言使用一组简单的字符串描述Path的图形,一般来说不需要学习它的语法,因为通常它是由工具生成的。
代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><Path Stroke="DarkGoldenRod" StrokeThickness="3" Data="M 100,200 C 100,25 400,350 400,175 H 280" />
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477247490.jpg)
通常用一个Path代替多个Shape不止更好管理,用户界面的性能也会更好。Blend里面提供了针对Shape的功能,可以对多个Shape进行合并或转换为路劲。
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477295804.jpg)
下面表格列出了Shape的各项属性,标记为“×”的属性代码这个属性对这个形状无效。
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477240152.jpg)
ViewBox是拉伸或缩放单个子元素的容器,最常用来搭配Shape(或文字)使用,因为Shape是矢量图形,放大后不会失真。
ViewBox有以下三个属性:
Child: 获取或设置 Viewbox 元素的单一子元素。Stretch: 获取或设置确定内容如何适合可用空间的 Stretch 模式。StretchDirection: 获取或设置确定缩放如何应用于 Viewbox 内容的 StretchDirection。代码语言:javascript代码运行次数:0运行复制<pre class="brush:php;toolbar:false;"><StackPanel Orientation="Horizontal"> <Canvas Width="30" Height="30"> <Rectangle Stroke="Red" StrokeThickness="1" Height="20" Width="20" /> <Rectangle Stroke="Green" StrokeThickness="1" Height="20" Width="20" Canvas.Left="10" Canvas.Top="10" /> </Canvas> <Viewbox Height="100" Width="100" Margin="30,0,0,0" Stretch="Uniform"> <Canvas Height="30" Width="30"> <Rectangle Stroke="Red" StrokeThickness="1" Height="20" Width="20" /> <Rectangle Stroke="Green" StrokeThickness="1" Height="20" Width="20" Canvas.Left="10" Canvas.Top="10" /> </Canvas> </Viewbox></StackPanel>
![[UWP]实用的Shape指南](https://img.php.cn/upload/article/001/503/042/175573477275066.jpg)
系统地学过Shape相关知识只在很多年前刚开始学WPF/Silverlight时做过,平时除了Rectangle和Line其他的Shape好少会用到,所以即使有多年经验对Shape的很多知识点还是有点陌生,这次趁学习UWP的机会复习一下。
但因为很多知识点平时就很少会用到,后面越写越腻味。
9. 参考绘制形状 Windows.UI.Xaml.Shapes 命名空间 Windows.UI.Xaml.Media 命名空间
以上就是[UWP]实用的Shape指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号