Search code examples
c#xamlxamarinxamarin.formsxamarin.android

How to change color on switch?


I have 2 projects. In both I style switch. After updating VS I have problems in my new project when I add

  <Switch>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="On">
                        <VisualState.Setters>
                            <Setter Property="ThumbColor"
                Value="#2D78FD" />
                            <Setter Property="OnColor"
                Value="#2D78FD" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Off">
                        <VisualState.Setters>
                            <Setter Property="ThumbColor"
                Value="LightGray" />
                            <Setter Property="OnColor"
                Value="LightGray" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Switch>

I understand that I have problems with themes On my phone I have Dark theme so I write

BackgroundColor="{AppThemeBinding Light=White, Dark=White}"

And then I have white BackgroundColor and have problems with the switch enter image description here

If I don`t write

BackgroundColor="{AppThemeBinding Light=White, Dark=White}"

enter image description here

Why my OnColor does not work at white BackgroundColor when it turns off(I tried other colors and only ThumbColor change color)


Solution

  • Please update your Xamarin.forms version to the latest 4.8.0.1364, I test your code and it works well.