且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone开发(33):路径之其它Geometry

更新时间:2022-08-13 09:10:54

原文: Windows Phone开发(33):路径之其它Geometry

上一节中,我们把最复杂的PathGeometry给干了,生剩下几个家伙就好办事了。一起来见见他们的真面目吧。

 

一、LineGeometry

 

这个几何图形就很简单了,一条线段,两个点——StartPoint And EndPoint。

一起来看看下面的例子。

        <Path Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,5" EndPoint="200,320"/>
            </Path.Data>
        </Path>


 

运行之后你会看到以下情景:

Windows Phone开发(33):路径之其它Geometry

 

 

 

 

二、RectangleGeometry

它呈现一人矩形的几何图形,Rect指示其中矩形的位置大小,在XAML中可以用4个数值表示,即X、Y、Width、Height;别外,RadiusX和RadiusY表示圆角在X轴和Y轴上的半径。看下面的例子。

        <Path Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="12,6,125,90" RadiusX="24" RadiusY="30"/>
            </Path.Data>
        </Path>


运行效果如下图所示。

Windows Phone开发(33):路径之其它Geometry

 

 

 

 

三、EllipseGeometry

 

表示一个椭圆的几何图形,Center属性为椭圆的中心点的坐标,RadiusX和RadiusY分别为X轴方向上和Y轴方向上的半径长度。看例子。

        <Path Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="100,180" RadiusX="55" RadiusY="120"/>
            </Path.Data>
        </Path>


 

运行效果如下:

Windows Phone开发(33):路径之其它Geometry

 

 

 

四、GeometryGroup

 

严格上说,它不属性一种几何图形,但它很有用,因为它可以同时包含N个几何图形,如下面例子所示。

        <Path Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <GeometryGroup>
                    <LineGeometry StartPoint="32,185" EndPoint="180,230"/>
                    <RectangleGeometry Rect="35,85,136,96" RadiusX="25" RadiusY="5"/>
                    <EllipseGeometry Center="112,130" RadiusX="45" RadiusY="36"/>
                </GeometryGroup>
            </Path.Data>
        </Path>


运行效是如下所示:

Windows Phone开发(33):路径之其它Geometry

 

 

 

下面是本节示例的完整XAML代码。

<phone:PhoneApplicationPage 
    x:Class="Sample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <Style TargetType="Path">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="20"/>
            <Setter Property="Stroke" Value="Blue"/>
            <Setter Property="StrokeThickness" Value="8"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Path Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,5" EndPoint="200,320"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="12,6,125,90" RadiusX="24" RadiusY="30"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="100,180" RadiusX="55" RadiusY="120"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <GeometryGroup>
                    <LineGeometry StartPoint="32,185" EndPoint="180,230"/>
                    <RectangleGeometry Rect="35,85,136,96" RadiusX="25" RadiusY="5"/>
                    <EllipseGeometry Center="112,130" RadiusX="45" RadiusY="36"/>
                </GeometryGroup>
            </Path.Data>
        </Path>
    </Grid>
</phone:PhoneApplicationPage>