1

You can set a gradient background inside the xaml for a page like this

<Button.Background>
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
        <GradientStop Color="White" Offset="0.6" />
        <GradientStop Color="Blue" Offset="1.0" />
    </LinearGradientBrush>
</Button.Background> 

but when I search for the format to use when doing this in a global style like

<ResourceDictionary>
  <Style TargetType="Button">
    <Setter Property="BorderColor" Value="Lime" />
    <Setter Property="BorderRadius" Value="10" />
    ......

I find nothing usable. It seems that is might not even be doable? Most links even seem to think there is no direct support for gradients without plugins, so perhaps it is a new addition to xamarin forms?

I specifically want to do this for buttons and contentpages, but I suspect the format will be the same for both - if it is supported at all.

Cfun
  • 8,442
  • 4
  • 30
  • 62
JoeTaicoon
  • 1,383
  • 1
  • 12
  • 28

1 Answers1

4

You can try with the following:

<ResourceDictionary>
    <Style TargetType="Button">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="White" Offset="0.6"/>
                    <GradientStop Color="Blue" Offset="1.0"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Keep in mind that currently Xamarin.forms is having an issue with global implicit style

Documentation: styles-and-templates

Cfun
  • 8,442
  • 4
  • 30
  • 62
  • That was fast! Thanks a lot for that. It works as hoped. Do wonder why I was unable to find any info about this, though. Is it a very new functionality? – JoeTaicoon Oct 09 '20 at 17:29
  • @JoeTaicoon No it is there from before, you are not setting your `Background` property as a simple value but as VisualTree instead that why this markup syntax has to be used. It has nothing to do with the gradient. – Cfun Oct 09 '20 at 17:51