How to change BackgroundColor using OnPlatform Xamarin

Viewed 3322

I am trying to change the background of an entry depending on the device it's running on. I tried to do it based on information from: https://developer.xamarin.com/guides/xamarin-forms/xaml/passing-arguments/

and got to this:

        <Entry TextColor="White"
            x:Name="entryTop"
            HeightRequest="40"
            WidthRequest="150"
            RelativeLayout.XConstraint = "{ConstraintExpression Type=RelativeToParent,
                Property=Width,
                Factor=.48,
                Constant=10}"
            RelativeLayout.YConstraint = "{ConstraintExpression Type=RelativeToParent,
                Property=Height,
                Factor=0,
                Constant=5}" >
            <Entry.BackgroundColor>
                <OnPlatform x:TypeArguments="x:String">
                    <On Platform="Android" Value="Transparent" />
                    <On Platform="iOS" Value="white" />
                </OnPlatform>
            </Entry.BackgroundColor>
        </Entry>

When I try to build my project, I get this error: No property, bindable property, or event found for BackgroundColor, or mismatching type between value and property.

I tried with color factory methods and several TypeArguments, but cannot seem to get the right ones.

1 Answers

I found the answer:

     <Entry.BackgroundColor>
         <OnPlatform x:TypeArguments="Color"
             Android="#5b5b5b"                 
             iOS="#fff">
         </OnPlatform>
     </Entry.BackgroundColor>

The TypeArgument Color receives only hex values when used this way. It won't work with RGB or static colors ("White", "Black", etc). I hope this helps.

Related