Forum Xamarin.Forms

DarkTheme vs Binding a Color

DimChrisDimChris USMember ✭✭✭✭

I want to create an option inside my application for dark theme. I don't want to change the theme automatically using the default device theme. Only when the user will select it.
So i am in face two options.
First option:

<StackLayout>
  <Frame BackgroundColor="{AppThemeBinding Light=Light, Dark=Dark}" />
</StackLayout>

Second option. Create a static variable and a class.

public class ThemeHelper
    {
        public static string ThemeName = "Dark";
        public static Color FrameBackgroundColor = InitTheme(ThemeName);

        private static Color InitTheme(string themName)
        {
            switch (themName)
            {
                case "Dark": return Color.Black;
                case "Light": return Color.White;
                default: return Color.White;
            }
        }
    }
}

And then isnide xml

<StackLayout>
    <Frame BackgroundColor="{x:Static local:ThemeHelper.FrameBackgroundColor}" />
</StackLayout>

Does second way has any performace issue? Second way seems more easy to me cause i can implement more than one theme.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Based on your description, the first option is better.

    Does second way has any performace issue?

    Yes, use static variables will take up part of the memory.

    AppThemeBinding along with UserAppTheme make it really easy to handle theme modes in your Xamarin.Forms apps. This works not only for colors, but images and other resources as well.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • DimChrisDimChris USMember ✭✭✭✭
    Thank LeonLu. Can i use more than two themes in appbindingtheme?? Example i need to create exept light and dark, another custom theme. Could you provide me an e ample?
Sign In or Register to comment.