Xamarin Forms LinearGradientBrush of Frame not working when colors are set from Dynamic Style

1.2k Views Asked by At

I am trying to use a Style to create a Frame with a linear gradient in it. If I hard code the colors in the style, the Frame shows correctly. If I attempt to bind the colors to properties in my custom app setting object, the style does not work. Below is an example:

Here is the static style. I configured it to use the same colors coming from the object to ensure there was nothing wrong with the values.

        <Style x:Key="wlHeaderGradient" TargetType="Frame">
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="Background">
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="#FF008080" Offset="0.0" />
                    <GradientStop Color="#FFFF6347" Offset="0.5" />
                    <GradientStop Color="#FF008080" Offset="1.0" />
                </LinearGradientBrush>
            </Setter>
        </Style>

Here is the Dynamic Style

        <Style x:Key="wlHeaderGradient1" TargetType="Frame">
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="Background">
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="{Binding HostPublicStyleInfo.CheckInHeaderBgColor1.ColorHex}" Offset="0.0" />
                    <GradientStop Color="{Binding HostPublicStyleInfo.CheckInHeaderBgColor2.ColorHex}" Offset="0.5" />
                    <GradientStop Color="{Binding HostPublicStyleInfo.CheckInHeaderBgColor1.ColorHex}" Offset="1.0" />
                </LinearGradientBrush>
            </Setter>
        </Style>

Here is the StackLayout I am using to test this:

        <StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="Start">
            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Start">
                <Frame x:Name="frmTest" Style="{StaticResource wlHeaderGradient}">
                </Frame>

                <Frame x:Name="frmTest1" Style="{StaticResource wlHeaderGradient1}">
                </Frame>
            </StackLayout>

            <Label x:Name="lblBGColor1" HorizontalOptions="Center" VerticalOptions="Center"/>
            <Label x:Name="lblBGColor2" HorizontalOptions="Center" VerticalOptions="Center"/>
            <Label x:Name="lblSampleWithStyle" Style="{StaticResource wlPublicHeader1}" Text="This is a working style with binding to same object." />
        </StackLayout>

Here is the code from the CodeBehind page were I display the CheckInHeaderBgColor1 and CheckInHeaderBgColor2 values. I run this code at the end of the OnAppearing event:

lblBGColor1.Text = App.gvm_AppSettings.HostPublicStyleInfo.CheckInHeaderBgColor1.ColorHex;
lblBGColor2.Text = App.gvm_AppSettings.HostPublicStyleInfo.CheckInHeaderBgColor2.ColorHex;

Here is the output I get:

enter image description here

I bind the entire page to my appseting in the CodeBehind as follows:

this.BindingContext = App.gvm_AppSettings;

NOTE: I use other properties from this AppSettings object in other styles on the page (as shown below) and they seem to work. In the screen shot, you can see the text color of the last label matches the background color of the screen below it.

Here is the style:

       <Style x:Key="wlPublicHeader1" TargetType="Label" >
            <Setter Property="BackgroundColor" Value="Transparent" />
            <Setter Property="Padding" Value="8,4,8,4" />
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="TextColor" Value="{Binding HostPublicStyleInfo.CheckInFormBgColor1.ColorHex}" />
            <Setter Property="FontSize" Value="Small" />
            <Setter Property="FontAttributes" Value="Bold" />
        </Style>

If anyone has an idea of what I am doing wrong I would greatly appreciate the feedback.

UPDATE::

Based on feedback below - I created a property of my custom color object that returned a Xamarin.Forms.Color object and attempted to bind to that property. It did not work. I also tried using dynamic properties in the App.Xaml file, modifying the value of those properties when my custom color object is loaded but that did not work either. Here are the updated samples:

App.Xaml Colors

<Application.Resources>
    <ResourceDictionary>
        <Color x:Key="grdHeaderBGColor1">White</Color>
        <Color x:Key="grdHeaderBGColor2">Black</Color>

Here are the three updated test styles:

       <Style x:Key="wlHeaderGradient" TargetType="Frame">
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="Background">
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="#FF008080" Offset="0.0" />
                    <GradientStop Color="#FFFF6347" Offset="0.5" />
                    <GradientStop Color="#FF008080" Offset="1.0" />
                </LinearGradientBrush>
            </Setter>
        </Style>

        <Style x:Key="wlHeaderGradient1" TargetType="Frame">
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="Background">
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="{Binding HostPublicStyleInfo.CheckInHeaderBgColor1.DisplayColor}" Offset="0.0" />
                    <GradientStop Color="{Binding HostPublicStyleInfo.CheckInHeaderBgColor2.DisplayColor}" Offset="0.5" />
                    <GradientStop Color="{Binding HostPublicStyleInfo.CheckInHeaderBgColor1.DisplayColor}" Offset="1.0" />
                </LinearGradientBrush>
            </Setter>
        </Style>

        <Style x:Key="wlHeaderGradient2" TargetType="Frame">
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
        </Style>

And here is the XAML to generate the sample Frames and Example 2 uses the Dynamic Resources.

        <StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="Start">
            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Start">
                <Frame x:Name="frmTest" Style="{StaticResource wlHeaderGradient}">
                </Frame>

                <Frame x:Name="frmTest1" Style="{StaticResource wlHeaderGradient1}">
                </Frame>

                <Frame x:Name="frmTest2" Style="{StaticResource wlHeaderGradient2}">
                    <Frame.Background>
                        <LinearGradientBrush EndPoint="0,1">
                            <GradientStop Color="{DynamicResource grdHeaderBGColor1}" Offset="0.0" />
                            <GradientStop Color="{DynamicResource  grdHeaderBGColor2}" Offset="0.5" />
                            <GradientStop Color="{DynamicResource grdHeaderBGColor1}" Offset="1.0" />
                        </LinearGradientBrush>
                    </Frame.Background>
                </Frame>

            </StackLayout>

            <Label x:Name="lblBGColor1" HorizontalOptions="Center" VerticalOptions="Center"/>
            <Label x:Name="lblBGColor2" HorizontalOptions="Center" VerticalOptions="Center"/>
            <Label x:Name="lblSampleWithStyle" Style="{StaticResource wlPublicHeader1}" Text="This is a working style with binding to same object." />
        </StackLayout>

The results are as follows:

Results screen show

The second sample - binding to a Xamarin.Forms.Color object does not work at all. The third example, attempting to use the dynamic resources never updates to the new colors (they stay the static black and white values) even though I have ensured the values are being changed when the custom color objects are being loaded.

So again I am at a loss here.

2

There are 2 best solutions below

0
On BEST ANSWER

The second sample - binding to a Xamarin.Forms.Color object does not work at all.

The second question is need to set Path to a sub-property to do the binding.

Set the Name of the page.

x:Name="root"

Style with binding:

 <Style x:Key="wlHeaderGradient1"  TargetType="Frame">
          
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="Background">
               
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="{Binding Path = BindingContext.CheckInHeaderBgColor1 ,Source={x:Reference root}}" Offset="0.0" />
                    <GradientStop Color="{Binding Path = BindingContext.CheckInHeaderBgColor2 ,Source={x:Reference root}}" Offset="0.5" />
                    <GradientStop Color="{Binding Path = BindingContext.CheckInHeaderBgColor1 ,Source={x:Reference root}}" Offset="1.0" />
                </LinearGradientBrush>
            </Setter>
        </Style>

Xaml:

   <StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="Start">
        <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Start">


            <Frame Style="{StaticResource wlHeaderGradient1}"  x:Name="frmTest1" >
            </Frame>

        </StackLayout>

    </StackLayout>

Code behind:

 public Page12()
    {
        InitializeComponent();
        this.BindingContext = new HostPublicStyleInfo();
    }

ViewModel:

public class HostPublicStyleInfo
{
    public HostPublicStyleInfo()
    {
        CheckInHeaderBgColor1 = "#FF008080";
        CheckInHeaderBgColor2 = "#FFFF6347";
        frmTest2Style = "frmTest2GradientStyle";
    }
    public string frmTest2Style { get; set; }
    public string CheckInHeaderBgColor1 { get; set; }
    public string CheckInHeaderBgColor2 { get; set; }


}

The third example, attempting to use the dynamic resources never updates to the new colors (they stay the static black and white values) even though I have ensured the values are being changed when the custom color objects are being loaded.

  1. Set the White-Black-White in Style instead of in Frame.

    <Style x:Key="wlHeaderGradient2" TargetType="Frame">
             <Setter Property="BorderColor" Value="Purple" />
             <Setter Property="HasShadow" Value="False" />
             <Setter Property="CornerRadius" Value="0" />
             <Setter Property="HeightRequest" Value="100" />
             <Setter Property="WidthRequest" Value="100"/>
             <Setter Property="HorizontalOptions" Value="Center" />
             <Setter Property="VerticalOptions" Value="Center" />
             <Setter Property="Background">
                 <LinearGradientBrush EndPoint="0,1">
                     <GradientStop Color="{DynamicResource grdHeaderBGColor1}" Offset="0.0" />
                     <GradientStop Color="{DynamicResource  grdHeaderBGColor2}" Offset="0.5" />
                     <GradientStop Color="{DynamicResource grdHeaderBGColor1}" Offset="1.0" />
                 </LinearGradientBrush>
             </Setter>
         </Style>
    

    And then you could cover it with the new style. frmTest2GradientStyle is the style which i set in Application.Resources.

    frmTest2.Style = (Style)Application.Current.Resources["frmTest2GradientStyle"];
    
  2. If you set the background in Frame like below, you could change the background instead of Background.

    <Frame x:Name="frmTest3" Style="{StaticResource wlHeaderGradient2}">
                 <Frame.Background>
                     <LinearGradientBrush EndPoint="0,1">
                         <GradientStop Color="{DynamicResource grdHeaderBGColor1}" Offset="0.0" />
                         <GradientStop Color="{DynamicResource  grdHeaderBGColor2}" Offset="0.5" />
                         <GradientStop Color="{DynamicResource grdHeaderBGColor1}" Offset="1.0" />
                     </LinearGradientBrush>
                 </Frame.Background>
             </Frame>
    

    Change the background in code:

         GradientStopCollection gradientStops = new GradientStopCollection();
         gradientStops.Add(new GradientStop() { Color = Color.Green, Offset = (float)0.0 });
         gradientStops.Add(new GradientStop() { Color = Color.Red, Offset = (float)0.5 });
         gradientStops.Add(new GradientStop() { Color = Color.Green, Offset = (float)1.0 });
    
         LinearGradientBrush linearGradientBrush = new LinearGradientBrush()
         {
             EndPoint = new Point(0, 1),
             GradientStops = gradientStops
         };
    
         frmTest3.Background = linearGradientBrush;
    
3
On

In my experience, I have found colors not binding properly when you try to bind them to string hex values like this. You could change the type of those string fields to color object and try.

Is your CheckInHeaderBgColor1 and CheckInHeaderBgColor2 properties of the type Color? If yes, try binding them directly

Dynamic colors will also work. Like this:

       <Style x:Key="wlHeaderGradient" TargetType="Frame">
            <Setter Property="BorderColor" Value="Purple" />
            <Setter Property="HasShadow" Value="False" />
            <Setter Property="CornerRadius" Value="0" />
            <Setter Property="HeightRequest" Value="100" />
            <Setter Property="WidthRequest" Value="100"/>
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="Background">
                <LinearGradientBrush EndPoint="0,1">
                    <GradientStop Color="{DyanmicResource Color1}" Offset="0.0" />
                    <GradientStop Color="{DyanmicResource Color2}" Offset="0.5" />
                    <GradientStop Color="{DyanmicResource Color3}" Offset="1.0" />
                </LinearGradientBrush>
            </Setter>
        </Style>