How to draw a line or create any image #25318
Replies: 1 comment
-
An IDrawable must be used with a GraphicsView. For more information see https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/graphicsview?view=net-maui-8.0. <GraphicsView Drawable="{StaticResource drawable}"
HeightRequest="300"
WidthRequest="400" /> I usually create an object that subclasses GraphicsView with an IDrawable interface. This will simplify the XAML usage. It also simplifies access between your object's bindable properties (including GraphicsView's Width and Height) with your IDrawable. // MyFirstDrawing.cs
public class MyFirstDrawing : GraphicsView, IDrawable
{
public MyFirstDrawing() { Drawable = this; }
public void Draw(ICanvas canvas, RectF dirtyRect)
{
#region Example of IDrawable accessing bindable properties from GraphicsView
canvas.FillColor = Colors.DarkBlue;
canvas.FillRectangle(0, 0, Width, Height);
#endregion
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawLine(10, 10, 90, 100);
}
} <!-- MainPage.xaml -->
<local:MyFirstDrawing HeightRequest="300" WidthRequest="400" /> However, I recommend against using GraphicsView for simple graphics. A much better starting point would be with Shapes https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/shapes/?view=net-maui-8.0, particularly Path https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/shapes/path?view=net-maui-8.0 and the Path markup syntax https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/shapes/path-markup-syntax?view=net-maui-8.0. <Path
Stroke="Red"
StrokeThickness="6"
Data="M 10 10 L 90 100"
HeightRequest="300"
WidthRequest="400" /> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I used below code but it's not working (can't see any line drawn while no error) if anything missing please let me know
MainPage.xaml
Beta Was this translation helpful? Give feedback.
All reactions