Search code examples
colorsbackgroundcontrolsmaui

Unable to set the background color of the GraphicsView control


Unable to change background color of GraphicsView control. I can change the background color of the ContentPage, but the background color of the GraphicsView assumes the color of the ContentPage. I tried to ensure the opacity of the GraphicsView is set to 1. I even tried to set the background color in the code behind. Even A.I. is failed me. It is giving me solutions that have nothing to do with GraphicsView. Microsoft examples on GraphicsView or canvas don’t change the background color. Any help please.

Backgrouind color of GraphicsView not changing color to yellow

XAML

<VerticalStackLayout>
    <Border StrokeThickness="0">
        <GraphicsView x:Name="canvas" 
                      Drawable="{StaticResource drawable}"
                      HeightRequest="500"
                      WidthRequest="500"
                      Background="Yellow"
                      BackgroundColor="Yellow"/>
    </Border>
</VerticalStackLayout>    

C# Code Behind

public void Draw(ICanvas canvas, RectF dirtyRect)
{
    canvas.FontColor = Colors.Red;
    canvas.FontSize = 18;
    canvas.DrawString("Text is left aligned.", 50, 20, 380, 100, HorizontalAlignment.Left, VerticalAlignment.Top);
    canvas.DrawString("Text is centered.", 20, 60, 380, 100, HorizontalAlignment.Center, VerticalAlignment.Top);
    canvas.DrawString("Text is right aligned.", 20, 100, 380, 100, HorizontalAlignment.Right, VerticalAlignment.Top);
    canvas.FillColor = Colors.Yellow;
    canvas.StrokeColor = Colors.Yellow;
}

Solution

  • This is a bug in Android at least on .NET 8, I have even raised a bug for the same that you can find in their Github repo https://github.com/dotnet/maui/issues/19568