我有XAML绘制箭头,我尝试将此XAML转换为代码失败: 上箭头是XAML,下面是代码。
有人能告诉我在代码中我做错了什么吗?我希望它看起来与XAML完全一样。
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle>
<Rectangle.Fill>
<DrawingBrush Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Geometry="F1 M 3.64665,5.62366C 5.73362,3.09485 8.89196,1.48303 12.4268,1.48303C 16.1132,1.48303 19.3901,3.23596 21.4694,5.95398">
<GeometryDrawing.Pen>
<Pen Thickness="2.4" LineJoin="Round" Brush="Black"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Geometry="F1 M 21.4719,1.21204L 21.4719,5.96594L 16.7181,5.96594">
<GeometryDrawing.Pen>
<Pen Thickness="2.4" MiterLimit="2.75" Brush="Black"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="Black" Geometry="F1 M 21.4593,0.00012207C 22.122,0.00012207 22.6593,0.537231 22.6593,1.20007C 22.6593,1.86267 22.122,2.40002 21.4593,2.40002C 20.7966,2.40002 20.2593,1.86267 20.2593,1.20007C 20.2593,0.537231 20.7966,0.00012207 21.4593,0.00012207 Z "/>
<GeometryDrawing Brush="Black" Geometry="F1 M 16.7121,4.76526C 17.3749,4.76526 17.9121,5.30261 17.9121,5.96545C 17.9121,6.62805 17.3749,7.16516 16.7121,7.16516C 16.0494,7.16516 15.5121,6.62805 15.5121,5.96545C 15.5121,5.30261 16.0494,4.76526 16.7121,4.76526 Z "/>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="img" Grid.Row="1"/>
</Grid>
代码背后:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
img.Source = _createInnerIcon();
}
private DrawingImage _createInnerIcon()
{
SolidColorBrush lineBrush = Brushes.Black;
var dGroup = new DrawingGroup();
using (DrawingContext dc = dGroup.Open())
{
dc.DrawGeometry(lineBrush, new Pen(lineBrush, 2.4) { LineJoin = PenLineJoin.Round }, Geometry.Parse("F1 M 3.64665,5.62366C 5.73362,3.09485 8.89196,1.48303 12.4268,1.48303C 16.1132,1.48303 19.3901,3.23596 21.4694,5.95398"));
dc.DrawGeometry(lineBrush, new Pen(lineBrush, 2.4) { MiterLimit = 2.75 }, Geometry.Parse("F1 M 21.4719,1.21204L 21.4719,5.96594L 16.7181,5.96594"));
dc.DrawGeometry(lineBrush, new Pen(lineBrush, 1), Geometry.Parse("F1 M 21.4593,0.00012207C 22.122,0.00012207 22.6593,0.537231 22.6593,1.20007C 22.6593,1.86267 22.122,2.40002 21.4593,2.40002C 20.7966,2.40002 20.2593,1.86267 20.2593,1.20007C 20.2593,0.537231 20.7966,0.00012207 21.4593,0.00012207 Z "));
dc.DrawGeometry(lineBrush, new Pen(lineBrush, 1), Geometry.Parse("F1 M 16.7121,4.76526C 17.3749,4.76526 17.9121,5.30261 17.9121,5.96545C 17.9121,6.62805 17.3749,7.16516 16.7121,7.16516C 16.0494,7.16516 15.5121,6.62805 15.5121,5.96545C 15.5121,5.30261 16.0494,4.76526 16.7121,4.76526 Z "));
}
return new DrawingImage(dGroup);
}
}
答案 0 :(得分:1)
在XAML中,您只为第3和第4个几何图形指定画笔(并且仅为第1和第2个画笔指定画笔),因此您的代码段实际上并不相等。
使用块中的那4行的小修复应该可以解决这个问题:
dc.DrawGeometry(null, new Pen(lineBrush, 2.4) { LineJoin = PenLineJoin.Round }, Geometry.Parse("F1 M 3.64665,5.62366C 5.73362,3.09485 8.89196,1.48303 12.4268,1.48303C 16.1132,1.48303 19.3901,3.23596 21.4694,5.95398"));
dc.DrawGeometry(null, new Pen(lineBrush, 2.4) { MiterLimit = 2.75 }, Geometry.Parse("F1 M 21.4719,1.21204L 21.4719,5.96594L 16.7181,5.96594"));
dc.DrawGeometry(lineBrush, null, Geometry.Parse("F1 M 21.4593,0.00012207C 22.122,0.00012207 22.6593,0.537231 22.6593,1.20007C 22.6593,1.86267 22.122,2.40002 21.4593,2.40002C 20.7966,2.40002 20.2593,1.86267 20.2593,1.20007C 20.2593,0.537231 20.7966,0.00012207 21.4593,0.00012207 Z "));
dc.DrawGeometry(lineBrush, null, Geometry.Parse("F1 M 16.7121,4.76526C 17.3749,4.76526 17.9121,5.30261 17.9121,5.96545C 17.9121,6.62805 17.3749,7.16516 16.7121,7.16516C 16.0494,7.16516 15.5121,6.62805 15.5121,5.96545C 15.5121,5.30261 16.0494,4.76526 16.7121,4.76526 Z "));