将XAML中的阴阳转换为C#

时间:2014-11-19 06:43:04

标签: c# xaml geometry

我试图使用C#代码绘制一个阴阳符号,因为(对我来说)感觉我可以更好地控制我正在做的事情,并且操作起来也更容易。

Yin Yang Symbol from XAML

我确实成功使用XAML绘制符号:

<Canvas Margin="50,50,900,1210" Grid.ColumnSpan="2">
    <Path Stroke="Black" Canvas.Top="52" Canvas.Left="76.248" Height="244" Stretch="Uniform" Width="344.504" Fill="White">
        <Path.Data>
            <PathGeometry>
                <PathFigure StartPoint="50,0" IsFilled="True">
                    <ArcSegment IsLargeArc="True" Size="50, 50" Point="50, 100" SweepDirection="Clockwise" />
                    <ArcSegment IsLargeArc="True" Size="25, 25" Point="50, 50" SweepDirection="Clockwise" />
                    <ArcSegment IsLargeArc="True" Size="25, 25" Point="50, 0" SweepDirection="Counterclockwise" />
                </PathFigure>
            </PathGeometry>
        </Path.Data>
    </Path>

    <Path Stroke="Black" Canvas.Top="52" Canvas.Left="14.748" Height="244" Stretch="Uniform" Width="344.504" Fill="Black">
        <Path.Data>
            <PathGeometry>
                <PathFigure StartPoint="50,0" IsFilled="True">
                    <ArcSegment IsLargeArc="True" Size="50, 50" Point="50, 100" SweepDirection="Counterclockwise" />
                    <ArcSegment IsLargeArc="True" Size="25, 25" Point="50, 50" SweepDirection="Clockwise" />
                    <ArcSegment IsLargeArc="True" Size="25, 25" Point="50, 0" SweepDirection="Counterclockwise" />
                </PathFigure>
            </PathGeometry>
        </Path.Data>
    </Path>

    <Path Stroke="Black" Canvas.Top="96" Canvas.Left="194" Height="50" Stretch="Uniform" Width="50" Fill="White" RenderTransformOrigin="1.126,-0.19">
        <Path.Data>
            <EllipseGeometry Center="10, 10" RadiusX="5" RadiusY=" 5">
                <EllipseGeometry.Transform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform CenterX="1" CenterY="10"/>
                        <RotateTransform CenterX="1" CenterY="10"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </EllipseGeometry.Transform>
            </EllipseGeometry>
        </Path.Data>
    </Path>

    <Path Stroke="Black" Canvas.Top="210" Canvas.Left="194" Height="50" Stretch="Uniform" Width="50" Fill="Black" RenderTransformOrigin="1.126,-0.19">
        <Path.Data>
            <EllipseGeometry Center="10, 10" RadiusX="5" RadiusY=" 5">
                <EllipseGeometry.Transform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform CenterX="1" CenterY="10"/>
                        <RotateTransform CenterX="1" CenterY="10"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </EllipseGeometry.Transform>

            </EllipseGeometry>
        </Path.Data>

    </Path>
</Canvas>

但是当我尝试使用代码绘制它时,我很困惑,为什么我没有得到任何东西。

Path YinYangPathLeft=new Path();
YinYangPathLeft.Stroke=Brushes.Black;
YinYangPathLeft.StrokeThickness=5;

Path YinYangPath = new Path();
YinYangPath.Stroke = Brushes.Black;
YinYangPath.StrokeThickness = 5;

Point topPoint=new Point(50,0);
Point middlePoint = new Point(50, 50);
Point bottomPoint = new Point(50, 100);

//point start point, size, rotation angle, bool is large arc, sweep direction, bool isstroked

//left side of symbol
ArcSegment arcSegment1 = new ArcSegment();
ArcSegment arcSegment2 = new ArcSegment();
ArcSegment arcSegment3 = new ArcSegment();

//right side of symbol
ArcSegment arcSegment4 = new ArcSegment();
ArcSegment arcSegment5 = new ArcSegment();
ArcSegment arcSegment6 = new ArcSegment();

//start of the segments for Yin Yang
arcSegment1.Point = new Point(bottomPoint.X, bottomPoint.Y);
arcSegment1.Size = new Size(100, 100);
arcSegment1.SweepDirection = SweepDirection.Counterclockwise;
arcSegment1.IsLargeArc = true;

arcSegment2.Point = new Point(middlePoint.X, middlePoint.Y);
arcSegment2.Size = new Size(50, 50);
arcSegment2.SweepDirection = SweepDirection.Clockwise;
arcSegment2.IsLargeArc = true;

arcSegment3.Point = new Point(topPoint.X, topPoint.Y);
arcSegment3.Size=new Size(50,50);
arcSegment3.SweepDirection=SweepDirection.Counterclockwise;
arcSegment3.IsLargeArc=true;

arcSegment4.Point = new Point(bottomPoint.X, bottomPoint.Y);
arcSegment4.Size = new Size(50, 50);
arcSegment4.SweepDirection = SweepDirection.Clockwise;
arcSegment4.IsLargeArc = true;

arcSegment5.Point = new Point(middlePoint.X, middlePoint.Y);
arcSegment5.Size = new Size(50, 50);
arcSegment5.SweepDirection = SweepDirection.Counterclockwise;
arcSegment5.IsLargeArc = true;

arcSegment6.Point = new Point(topPoint.X, topPoint.Y);
arcSegment6.Size = new Size(100, 100);
arcSegment6.SweepDirection = SweepDirection.Counterclockwise;
arcSegment6.IsLargeArc = true;
//End of segments for Yin Yang


PathFigure YinYangFigureLeft=new PathFigure();
YinYangFigureLeft.StartPoint = topPoint;
YinYangFigureLeft.IsClosed = true;
YinYangFigureLeft.Segments.Add(arcSegment1);
YinYangFigureLeft.Segments.Add(arcSegment2);
YinYangFigureLeft.Segments.Add(arcSegment3);

PathGeometry YinYangGeometryLeft=new PathGeometry();
//YinYangGeometryLeft.AddGeometry(YinYangGeometryLeft);
YinYangGeometryLeft.Figures.Add(YinYangFigureLeft);

PathFigure YinYangFigureRight = new PathFigure();
YinYangFigureRight.StartPoint = topPoint;
YinYangFigureRight.IsClosed = true;
YinYangFigureRight.Segments.Add(arcSegment4);
YinYangFigureRight.Segments.Add(arcSegment5);
YinYangFigureRight.Segments.Add(arcSegment6);

PathGeometry YinYangGeometryRight = new PathGeometry();
//YinYangGeometryRight.AddGeometry(YinYangGeometryRight);
YinYangGeometryRight.Figures.Add(YinYangFigureRight);

GeometryGroup YinYangGeometryGroup=new GeometryGroup();
YinYangGeometryGroup.Children.Add(YinYangGeometryLeft);
YinYangGeometryGroup.Children.Add(YinYangGeometryRight);

//YinYangPath.Data = YinYangGeometryGroup;

GeometryDrawing YinYangGeometryDrawing = new GeometryDrawing();
YinYangGeometryDrawing.Brush = Brushes.Black;
YinYangGeometryDrawing.Pen = new Pen(Brushes.Yellow, 2);
YinYangGeometryDrawing.Geometry = YinYangGeometryGroup;

//DrawingGroup= Yin+Yang
DrawingGroup YinYangDrawingGroup = new DrawingGroup();
YinYangDrawingGroup.Children.Add(YinYangGeometryDrawing);

//DrawingImage YinYangDrawingImage = new DrawingImage(YinYangGeometryDrawing);
DrawingImage YinYangDrawingImage = new DrawingImage(YinYangDrawingGroup);

YinYangTest.Source = YinYangDrawingImage;

我也尝试使用Canvas和StackPanel,但仍然是空的:

 /*
            Canvas YinYangCanvas = new Canvas();
            YinYangCanvas.Height = 200;
            YinYangCanvas.Children.Add(YinYangPath);
            this.Content = YinYangCanvas;
            */
            /*
            StackPanel YYSP = new StackPanel();
            YYSP.Children.Add(YinYangPath);
            this.Content = YYSP;
            */

1 个答案:

答案 0 :(得分:0)

出于对你正在努力完成的事情的好奇心,我想我会分享如何做到这一点,或许可以借助你如何更轻松地实现它的想法。

xaml yin yang

<Canvas Height="120" Width="120">

  <Path StrokeThickness="1.0" Stroke="Black" Fill="White" 
        Data="F1 M 89.591,29.017 C 89.591,52.492 72.658,59.419 56.110,59.419 C 41.871,59.419 30.711,75.583 30.711,87.512 C 30.711,102.873 41.258,112.244 51.418,117.710 C 54.023,118.063 56.679,118.261 59.381,118.261 C 91.899,118.261 118.261,91.899 118.261,59.381 C 118.261,29.019 95.279,4.033 65.764,0.851 C 82.748,7.431 89.591,19.831 89.591,29.017 Z"/>

  <Path Fill="Black" 
        Data="F1 M 0.500,59.381 C 0.500,89.196 22.668,113.820 51.418,117.710 C 41.258,112.244 30.711,102.873 30.711,87.512 C 30.711,75.583 41.871,59.419 56.110,59.419 C 72.658,59.419 89.591,52.492 89.591,29.017 C 89.591,19.831 82.748,7.431 65.764,0.851 C 63.667,0.624 61.538,0.500 59.381,0.500 C 26.862,0.500 0.500,26.861 0.500,59.381 Z"/>    

  <Path Fill="White" 
        Data="F1 M 53.233,30.594 C 53.233,27.065 56.094,24.206 59.621,24.206 C 63.148,24.206 66.009,27.065 66.009,30.594 C 66.009,34.122 63.148,36.981 59.621,36.981 C 56.094,36.981 53.233,34.122 53.233,30.594 Z"/>    

  <Path Fill="Black" 
        Data="F1 M 52.962,88.467 C 52.962,84.939 55.822,82.080 59.350,82.080 C 62.877,82.080 65.737,84.939 65.737,88.467 C 65.737,91.996 62.877,94.855 59.350,94.855 C 55.822,94.855 52.962,91.996 52.962,88.467 Z"/>

</Canvas> 

或者,如果您只想使用弧段,我将从一个椭圆开始,并在其上方仅使用弧段绘制一侧。至于为什么你的代码没有产生任何东西,我会问你是否有任何类型的错误,以及你有PaintEventArgs or CreateGraphics method or a Graphics Object etc哪里可以产生它。