xamarinxamarin.formsxamarin.androidxamarin.ioslineargradientbrush

How can I set LinearGradientBrush as the background for of ContentPages?


How can I add LinearGradientBrush as ContentPage.Background in App.xaml's ResourceDictionary? My code pasted below:

<LinearGradientBrush EndPoint="0,1">
    <GradientStop Color="LightBlue"
                  Offset="0.1" />
    <GradientStop Color="DarkBlue"
                  Offset="1.0" />
</LinearGradientBrush>

Solution

  • As LinearGradientBrush is a preview concept,don't forgget to add below codes in your App.xaml.cs.

    Device.SetFlags(new string[] {"Brush_Experimental" });
    

    and add below to the App.xaml:

    <Application.Resources>
        <ResourceDictionary>
            <Style  TargetType="ContentPage" ApplyToDerivedTypes="True">
                <Setter Property="Background">
                        <LinearGradientBrush EndPoint="0,1">
                            <GradientStop Color="LightBlue"
                  Offset="0.1" />
                            <GradientStop Color="DarkBlue"
                  Offset="1.0" />
                        </LinearGradientBrush>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>