System.Windows.Media 命名空间


.NET Framework 类库
StreamGeometry 类

更新:2007 年 11 月

定义几何形状,并使用 StreamGeometryContext 进行描述。此几何图形是 PathGeometry 的轻量替代图形:它不支持数据绑定、动画或修改。

命名空间:  System.Windows.Media
程序集:  PresentationCore(在 PresentationCore.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation

语法

Visual Basic(声明)
<TypeConverterAttribute(GetType(GeometryConverter))> _
Public NotInheritable Class StreamGeometry _
    Inherits Geometry
Visual Basic (用法)
Dim instance As StreamGeometry
C#
[TypeConverterAttribute(typeof(GeometryConverter))]
public sealed class StreamGeometry : Geometry
Visual C++
[TypeConverterAttribute(typeof(GeometryConverter))]
public ref class StreamGeometry sealed : public Geometry
J#
/** @attribute TypeConverterAttribute(GeometryConverter) */
public final class StreamGeometry extends Geometry
JScript
public final class StreamGeometry extends Geometry
XAML 对象元素用法
<StreamGeometry .../>
XAML 属性用法
<object property="moveAndDrawCommands"/>
XAML 值
moveAndDrawCommands

一个或多个移动和绘制命令。请参见 路径标记语法

备注

当需要描绘复杂的几何图形,但不希望为支持数据绑定、动画或修改而导致系统开销时,可使用 StreamGeometry。由于它具有高效性,因此 StreamGeometry 类是描绘装饰器的理想选择。

如果 StreamGeometry 包含 Transform 或任何未绘制或未填充的线段,则不能对其进行序列化。

Freezable 功能

StreamGeometry 是一种 Freezable 类型。有关 Freezable 功能(例如冻结和克隆)的信息,请参见 Freezable 对象概述

示例

StreamGeometryPathGeometry 的一个轻型替代品,可用于创建几何形状。当您需要描绘复杂的几何图形,但不希望因为支持数据绑定、动画或修改而引入系统开销时,可使用 StreamGeometry。例如,由于具有高效率,StreamGeometry 类是描绘装饰物的理想选择。

下面的示例使用属性语法用 XAML 创建一个三角形 StreamGeometry

XAML
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel>

    <Path Data="F0 M10,100 L100,100 100,50Z" 
      StrokeThickness="1" Stroke="Black"/>

  </StackPanel>
</Page>

有关 StreamGeometry 属性语法的更多信息,请参见路径标记语法页。

下一个示例使用 StreamGeometry 通过代码定义一个三角形。首先,该示例创建一个 StreamGeometry,然后获取一个 StreamGeometryContext 并使用它来描绘三角形。

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    // Use StreamGeometry with StreamGeometryContext to define a triangle shape.
    public partial class StreamGeometryTriangleExample : Page
    {
        public StreamGeometryTriangleExample()
        {
            // Create a path to draw a geometry with.
            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;

            // Create a StreamGeometry to use to specify myPath.
            StreamGeometry geometry = new StreamGeometry();
            geometry.FillRule = FillRule.EvenOdd;

            // Open a StreamGeometryContext that can be used to describe this StreamGeometry 
            // object's contents.
            using (StreamGeometryContext ctx = geometry.Open())
            {

                // Begin the triangle at the point specified. Notice that the shape is set to 
                // be closed so only two lines need to be specified below to make the triangle.
                ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */);

                // Draw a line to the next specified point.
                ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);

                // Draw another line to the next specified point.
                ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */);
            }

            // Freeze the geometry (make it unmodifiable)
            // for additional performance benefits.
            geometry.Freeze();

            // Specify the shape (triangle) of the Path using the StreamGeometry.
            myPath.Data = geometry;

            // Add path shape to the UI.
            StackPanel mainPanel = new StackPanel();
            mainPanel.Children.Add(myPath);
            this.Content = mainPanel;
        }
    }
}

下一个示例创建这样一个方法,该方法使用 StreamGeometryStreamGeometryContext 基于指定的参数来定义一个几何形状。

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class StreamGeometryExample : Page
    {
        public StreamGeometryExample()
        {
            // Create a path to draw a geometry with.
            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;

            // Create a StreamGeometry to use to specify myPath.
            StreamGeometry theGeometry = BuildRegularPolygon(new Point(200, 200), 200, 8, 0);
            theGeometry.FillRule = FillRule.EvenOdd;

            // Freeze the geometry (make it unmodifiable)
            // for additional performance benefits.
            theGeometry.Freeze();

            // Use the StreamGeometry returned by the BuildRegularPolygon to 
            // specify the shape of the path.
            myPath.Data = theGeometry;

            // Add path shape to the UI.
            StackPanel mainPanel = new StackPanel();
            mainPanel.Children.Add(myPath);
            this.Content = mainPanel;
        }

        StreamGeometry BuildRegularPolygon(Point c, double r, int numSides, double offsetDegree)
        {
            // c is the center, r is the radius,
            // numSides the number of sides, offsetDegree the offset in Degrees.
            // Do not add the last point.

            StreamGeometry geometry = new StreamGeometry();

            using (StreamGeometryContext ctx = geometry.Open())
            {
                ctx.BeginFigure(new Point(), true /* is filled */, true /* is closed */);

                double step = 2 * Math.PI / Math.Max(numSides, 3);
                Point cur = c;

                double a = Math.PI * offsetDegree / 180.0;
                for (int i = 0; i < numSides; i++, a += step)
                {
                    cur.X = c.X + r * Math.Cos(a);
                    cur.Y = c.Y + r * Math.Sin(a);
                    ctx.LineTo(cur, true /* is stroked */, false /* is smooth join */);
                }
            }

            return geometry;
        }
    }
}
继承层次结构

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows..::.Freezable
        System.Windows.Media.Animation..::.Animatable
          System.Windows.Media..::.Geometry
            System.Windows.Media..::.StreamGeometry
线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.5、3.0
另请参见

参考

其他资源

标记 :


Page view tracker