MAUI Draw line on canvas during runtime after button click

Viewed 13

I want to draw a new line in a GraphicsView after the click of a button. This will draw the line on startup but I would like to either redraw the current line or preferably add another line to the view.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:CoachesToolbox.Controls"
             xmlns:viewmodel="clr-namespace:CoachesToolbox.ViewModel"
             xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:drawable="clr-namespace:CoachesToolbox.Models"
             x:DataType="viewmodel:MainViewModel"
             x:Class="CoachesToolbox.Views.MainPage"
             Title="CoachesToolBox demos"
             >
    <ContentPage.Resources>
        <drawable:Drawable x:Key="MyDrawable" />

    </ContentPage.Resources>
<GraphicsView x:Name="Canvas"
                          HorizontalOptions="Fill"
                          VerticalOptions="Fill"
                          Drawable="{StaticResource MyDrawable}"
                          Grid.Column="1"
                          Grid.Row="0"
                          BackgroundColor="Transparent"/>
namespace CoachesToolbox.Models
{
    public class Drawable : IDrawable
    {
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            canvas.StrokeColor = Colors.Red;
            canvas.StrokeSize = 5;
            canvas.DrawLine(10, 10, 90, 100);
        }
    }
}
0 Answers
Related