简体   繁体   English

为什么我不能在XAML中使用Transform定义几何?

[英]Why can't I define a Geometry with a Transform in XAML?

I am trying to create a ResourceDictionary á la this answer that contains StreamGeometries that have Transforms set: 我正在尝试创建一个ResourceDictionary 这个答案包含具有Transforms集的StreamGeometries

<ResourceDictionary>
    <StreamGeometry x:Name="Chevrons">
        <StreamGeometry.Transform>
            <TranslateTransform X="20" Y="120"/>
        </StreamGeometry.Transform>
        M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 17 [...]
    </StreamGeometry>
</ResourceDictionary>

However, I get the following error: 但是,我收到以下错误:

1: Cannot add content to an object of type "StreamGeometry". 1:无法向“StreamGeometry”类型的对象添加内容。

and

2: TypeConverter syntax error encountered while processing initialization string ' {PathData} '. 2:处理初始化字符串' {PathData} '时遇到TypeConverter语法错误。 Element attributes are not allowed on objects created via TypeConverter. 通过TypeConverter创建的对象不允许使用元素属性。

So I tried it with a PathGeometry and got this error: 所以我用PathGeometry尝试了它并得到了这个错误:

The specified value cannot be assigned to the collection. 无法将指定的值分配给集合。 The following type was expected: "PathFigure". 预期以下类型:“PathFigure”。

Is there any way to do apply a transform to a Geometry in XAML code? 有没有办法在XAML代码中将变换应用于几何? Or do I just have to do it via code? 或者我只需要通过代码来完成它?

You may write it like this: 你可以这样写:

<PathGeometry x:Key="Chevrons">
    <PathGeometry.Transform>
        <TranslateTransform X="20" Y="120"/>
    </PathGeometry.Transform>
    <PathGeometry.Figures>
        M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 ...
    </PathGeometry.Figures>
</PathGeometry>

or like this: 或者像这样:

<PathGeometry x:Key="Chevrons"
    Figures="M21.750001,94.749999 L34.000002,117.66218 30.625003,133.62501 ...">
    <PathGeometry.Transform>
        <TranslateTransform X="20" Y="120"/>
    </PathGeometry.Transform>
</PathGeometry>

About the behavior of StreamGeometry , quote from MSDN : 关于StreamGeometry的行为,引自MSDN

Here: 这里:

A StreamGeometry is a Freezable type. StreamGeometry是Freezable类型。 StreamGeometry is light-weight alternative to PathGeometry for creating geometric shapes. StreamGeometry是PathGeometry的轻量级替代品,用于创建几何形状。 Use a StreamGeometry when you need to describe a complex geometry but do not want the overhead of supporting data binding, animation, or modification. 当您需要描述复杂几何体但不希望支持数据绑定,动画或修改的开销时,请使用StreamGeometry。 Because of its efficiency, the StreamGeometry class is a good choice for describing adorners. 由于其效率,StreamGeometry类是描述装饰者的不错选择。

And here: 和这里:

A StreamGeometry cannot be serialized if it contains a Transform or any non-stroked or unfilled segments. 如果StreamGeometry包含Transform或任何非描边或未填充的段,则无法序列化。

Therefore, use the PathGeomerty , as advised @Clemens. 因此,请使用PathGeomerty ,如@Clemens所建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM