4

Using Visual Studio Community Edition 2022 to build a .Net MAUI project.

I tried searching the official docs, but did not find an answer. Can someone explain the following?

Questions:

  1. What is the difference between Background and BackgroundColor?

  2. If neither (Background or BackgroundColor) is present, does the element inherit the background color from its parent?

Any other information with regard to how background color is determined would be appreciated.

If there is a page that answers the above question in the official docs, please post.

Cfun
  • 8,442
  • 4
  • 30
  • 62
bdcoder
  • 3,280
  • 8
  • 35
  • 55

1 Answers1

7
  1. The difference reside on the type of each property:
  • Background is of type Microsoft.Maui.Controls.Brush or any other type that sub class Brushlike SolidColorBrush, LinearGradientBrush and RadialGradientBrush. Some properties especially used with Shapes (Rectangle, ellipse...) such Fill, Stroke... are of type Brush. With Brush you can do some advanced area's painting like with gradients.

  • BackgroundColor is of type Microsoft.Maui.Graphics.Color.

I think SolidColorBrush is pretty close to Color.

Brushes docs.

  1. Technically it won't inherit the property value from it parent. Even that in cases where the default value (when the property is not set and value set by MAUI) of BackgroundColor is Transparent, could let believe that the BackgroundColor got inherited from the parent, but in fact it just shows the color underneath it; as @ToolmakerSteve explained in his comment; (could be the parent or any other element beneath in case of overlapped Grid children or using ZIndex).
Cfun
  • 8,442
  • 4
  • 30
  • 62
  • 1
    Re #2, it depends on what the app's default styles are. IMHO, Frame was not the best choice for this test. Many (most?) elements default to BackgroundColor="Transparent". That is they "show the color underneath". Which visually does look as if "the element inherits its background color from the parent". For there to be some different color, there must be some style or code somewhere, setting a different color. – ToolmakerSteve Oct 11 '22 at 23:53
  • @ToolmakerSteve I was not sure and was searching where and how MAUI might set the default value of this property for each control, thank you for the explanation. – Cfun Oct 12 '22 at 00:27
  • 1
    `Brush` can also take values of Type `Color`, So you can Assign a White Color to a property of Type Brush... – FreakyAli Oct 12 '22 at 06:10
  • 1
    @FreakyAli good point, under the hood it [performs a conversion](https://github.com/dotnet/maui/blob/main/src/Controls/src/Core/Brush.cs#L15) to return a `SolidColorBrush` which also sub class Brush. – Cfun Oct 12 '22 at 10:30
  • With regard to visual controls is there any (official) documentation on what the default Background / BackgroundColors might be? – bdcoder Oct 12 '22 at 19:36
  • In the docs they don't mention all the default values of properties. An example of xamarin VisualElement https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.visualelement.backgroundcolor?view=xamarin-forms . keep in mind that the framework could change this property at each control level subclassing VisualElement. Overall i think most if not all controls have Transparent as a default value. – Cfun Oct 12 '22 at 19:46
  • Thanks for the feedback / comments. I will mark your response as the answer with regard to question 1. With regard question 2 and any other information, as of today (15-Oct-2022), there is no official documentation that shows what values are used for BackgroundColor -- I guess the "answer" for those is: Try and see. – bdcoder Oct 15 '22 at 17:03