Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Set Color Property based on Dark/Light theme

ganesh96ganesh96 Member ✭✭✭

I'm using the Color Property to set colors for list items like this:

Each list item color is then set to Red or Black.

Now I'm working on Light/Dark theme handling for Android & iOS, which I do in App.xaml:

How can I set the colors in C# to Yellow and White instead of Red and Black in this code?

Tagged:

Best Answer

  • ganesh96ganesh96 Member ✭✭✭
    Accepted Answer

    I solved it another way, which can be used depending on what someone wants. I use this now:

    Instead of creating one listview style I created two styles: one for the dark theme and one for the light theme (both with their own binding):

    Then in the listview itself I simply point to one of the two, based on the system settings:

    That also does the trick.

Answers

  • ganesh96ganesh96 Member ✭✭✭

    EDIT:

    I divided the Color property into a Color_Light and Color_Dark property, then set it like this in App.xaml:

    This doesn't work however, since all list items remain black. How can I do this?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited September 22

    Currently AppThemeBinding does not support for binding on Light, Dark mode , we can only set static value on Light/Dark mode .

    If you want different colors on each platform ,refer to the solution here .

  • ganesh96ganesh96 Member ✭✭✭

    Hi @ColeX

    I need this feature cross-platform, is this the only way to achieve this?

  • ColeXColeX Member, Xamarin Team Xamurai

    Yes , since AppThemeBinding only supports static value i'm afraid this is the only way to achieve it on multiple platforms.

    You could raise feature request on github : https://github.com/xamarin/Xamarin.Forms/issues.

  • ganesh96ganesh96 Member ✭✭✭
    Accepted Answer

    I solved it another way, which can be used depending on what someone wants. I use this now:

    Instead of creating one listview style I created two styles: one for the dark theme and one for the light theme (both with their own binding):

    Then in the listview itself I simply point to one of the two, based on the system settings:

    That also does the trick.

Sign In or Register to comment.