简体   繁体   中英

How can i draw to canvas with C# in my xaml project?

I currently have below code:

Path path = new Path();
path.Fill = new RadialGradientBrush(Colors.White, Colors.Transparent)
{
    GradientOrigin = new Point(.5, .5),
    Center = new Point(.5, .5),
    RadiusX = 1.5,
    RadiusY = 1.5
};

PathGeometry geo = new PathGeometry();

PathFigure figure = new PathFigure();
figure.IsClosed = true;
figure.StartPoint = new Point(10, 10);

figure.Segments.Add(new LineSegment()
{
    Point = new Point(20, 20)
});

figure.Segments.Add(new LineSegment()
{
    Point = new Point(30, 10)
});

geo.Figures.Add(figure);
path.Data = geo;

Canvas.Children.Add(path);

But what is wrong with it? Nothing turns up. I'm expecting a black triangle with a white center gradient brush to its size limits.

Anyone have a clue?

Try this:

path.Fill = new RadialGradientBrush(Colors.Black, Colors.Transparent)
{
    GradientOrigin = new Point(.5, .5),
    Center = new Point(.5, .5),
    RadiusX = 1.5,
    RadiusY = 1.5
};

One more point: Your code works fine. The problem must be with your last line. Is Canvas the name of your canvas? Canvas is type but here you are using it as a variable.

您应该为路径设置“ Stroke ”,否则三角形将没有黑色部分:

path.Stroke = new SolidColorBrush(Colors.Black);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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