简体   繁体   English

将XAML中的阴阳转换为C#

[英]Converting a Yin Yang in XAML into C#

I'm trying to draw a Yin Yang symbol using C# code because (to me) it feels like I have better control over what I'm doing and it also feels easier to manipulate it. 我正在尝试使用C#代码绘制阴阳符号,因为(对我而言)感觉像我对自己的工作有更好的控制,并且感觉也更容易操作。

XAML的阴阳符号

I did succeed in drawing the symbol using 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>

but when I try to draw it using code, I'm so confused as to why I'm not getting anything. 但是当我尝试使用代码来绘制它时,我很困惑为什么我什么也没得到。

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;

I did also try using a Canvas and a StackPanel but still came up empty: 我确实也尝试过使用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;
            */

Out of curiosity towards what you're trying to accomplish, I figured I would share how I would do it to maybe lend on the idea of how you might do it a bit easier your way also. 出于对您要完成的任务的好奇心,我想我会分享我将如何做,以使您对自己的方式也可能会更容易一点有所帮助。

阴阳

<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> 

Alternatively if you just wanted to use arc segments, I'd start with one ellipse, and draw just one side on top of it with arc segment. 另外,如果您只想使用弧线段,我将从一个椭圆开始,然后用弧线段在椭圆的顶部绘制一侧。 As for why your code doesn't produce anything, I'd ask if you get any kind of errors and where you have PaintEventArgs or CreateGraphics method or a Graphics Object etc which would give a place to produce it. 至于为什么您的代码不产生任何东西,我想问一下您是否遇到任何类型的错误,以及在哪里可以使用PaintEventArgs或CreateGraphics方法或Graphics Object等位置。

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

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