Search code examples
maui

Local styling of Switch Control is not working properly


My understanding is that local styling has the precedence. But for some reason, the default template at the App level appears to be overriding the local style. For example, setting these local properties doesn't work or overridden:

<Switch OnColor="Orange" ThumbColor="Green" />

Adding a page level resource style doesn't work properly either:

<ContentPage.Resources>
    <Style TargetType="Switch">
        <Setter Property="OnColor" Value="Blue"  />
        <Setter Property="ThumbColor" Value="Red" />
    </Style>
</ContentPage.Resources>

<Switch />

Only when commenting the <Setter Property="VisualStateManager.VisualStateGroups"> at the app level, the above styles work.

Could this be a bug, or am I missing something here?


Solution

  • Yes, I can confirm that it's a potential issue on .NET 8 which is related to Switch control ignores OnColor and ThumbColor #19380. Fortunately, it's working on .NET 7.

    As an alternative workaround, you can fix it by commenting <Setter Property="VisualStateManager.VisualStateGroups"> as you said in your post, or you can use a CustomSwitchHandler which uses a CustomSwitch that derived from Switch. For more details, you can refer to this solution provided by MAUIoxo.