我在资源字典中覆盖了几个 XAML 定义的绘图组,并注意到一些奇怪的行为。如果我这样定义一个 DrawingImage :
<DrawingImage x:Key="My_Icon">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<StaticResource ResourceKey="Background" />
<StaticResource ResourceKey="Foreground" />
<DrawingGroup>
<!-- some content here for an overlay -->
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
在设计时一切正常,但在运行时出现异常:
'XAML 节点流:'System.Windows.Markup.StaticResourceHolder' 的值必须遵循 StartObject 和 StartMember。行号“14”和行位置“22”。
但是,如果我在静态资源之前插入一个空的 DrawingGroup 标记,一切正常:
<DrawingImage x:Key="My_Icon">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<DrawingGroup />
<StaticResource ResourceKey="Background" />
<StaticResource ResourceKey="Foreground" />
<DrawingGroup>
<!-- some content here for an overlay -->
</DrawingGroup>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
有谁知道为什么会这样?我假设这是一个通用的 XAML 节点处理错误,而不是特定于 DrawingGroups,但我可能是错的......
请注意,有问题的 StaticResources 是 DrawingGroups:
<DrawingGroup x:Key="Background">
<DrawingGroup.Children>
<GeometryDrawing Geometry="M 3,1.00001L 29,1.00001C 30.1046,1.00001 31,1.89544 31,3.00001L 31,29C 31,30.1046 30.1046,31 29,31L 3,31C 1.89543,31 1,30.1046 1,29L 1,3.00001C 1,1.89544 1.89543,1.00001 3,1.00001 Z ">
<GeometryDrawing.Pen>
<Pen LineJoin="Round" Brush="#FFEB8E00"/>
</GeometryDrawing.Pen>
<GeometryDrawing.Brush>
<RadialGradientBrush RadiusX="0.516666" RadiusY="0.516666" Center="0.499999,0.966667" GradientOrigin="0.499999,0.966667">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#FFFFECA8" Offset="0"/>
<GradientStop Color="#FFFFCD52" Offset="0.2"/>
<GradientStop Color="#FFFFCD52" Offset="0.669767"/>
<GradientStop Color="#FFFFB100" Offset="0.95814"/>
</RadialGradientBrush.GradientStops>
<RadialGradientBrush.RelativeTransform>
<TransformGroup/>
</RadialGradientBrush.RelativeTransform>
</RadialGradientBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing Brush="#67F9F0AD" Geometry="M 4,19L 28,19C 29.1046,19 30,19.8954 30,21L 30,28C 30,29.1046 29.1046,30 28,30L 4,30C 2.89543,30 2,29.1046 2,28L 2,21C 2,19.8954 2.89543,19 4,19 Z "/>
</DrawingGroup.Children>
</DrawingGroup>
我的用法很简单——我将整个 DrawingImage 绑定到 Infragistics WPF 按钮控件:
<igWPF:ButtonTool igWPF:RibbonGroup.MaximumSize="ImageAndTextLarge" Caption="New Item" SmallImage="{StaticResource My_Icon}" LargeImage="{StaticResource My_Icon}" />