How to implement custom theme to Xamarin Forms

Is there a way to add themes to a Xamarin Forms app that can load while the app is running and editable by the user by preference?

Currently I thought of maybe adding some sort of color converter to each component.

However this can get pretty messy fast, and wont be as flexible to more than two themes.
I saw some Xamarin theme stuff online where they swap out the resources depending on the flag on run time, but that would require the use to restart.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Is this you want ?

    Code

     <ContentPage.Resources>
            <ResourceDictionary>
                <local:StringToColorConverter x:Key="StringToColorConverter"/>
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <StackLayout Margin="50">
            <Entry x:Name="entry"/>
            <Label Text="I'm test" BindingContext="{x:Reference entry}" TextColor="{Binding Text,Converter={StaticResource StringToColorConverter}}"/>
        </StackLayout>
    
     public class StringToColorConverter : IValueConverter
        {
    
            Color lastColor = Color.Black;
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                System.Drawing.Color color = System.Drawing.Color.FromName((string)value);
                if(!(color.R == 0 && color.G == 0 && color.B == 0 && color.A == 0))
                {
                    lastColor = new Color(color.R, color.G, color.B, color.A);                             
                }
                return lastColor;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return "";
            }
        }
    
Sign In or Register to comment.