how to change xamarin.forms Theme programmatically/at runtime

JustJust DKMember ✭✭

Hey how do i change my Xamarin forms theme at runtime from light to dark etc?
Without overriding my Resources Dictionary?

Answers

  • NPCNPC USMember ✭✭

    I am unaware of what a Resources Dictionary is, but:

    I have a static class that stores/accesses the current theme within App.Current.Properties

    The constructor for all my pages gets it's theme from the class

    I have a Modal Settings page that allows me to set the theme from the static class

    Using a singleton NavigationService class I have a method that sets a new root by:
    Placing a new root page before current root
    Popping to root

    Once the Modal settings page is popped, it reveals the new root page which has been created with newly set theme

  • JustJust DKMember ✭✭

    Could you show me some code example?

  • JustJust DKMember ✭✭

    Nevermind i found out the solution to my problem.

  • jariwalahetaljariwalahetal USMember ✭✭

    @just

    Can you post your solution here?

  • tribaltribal USMember ✭✭✭

    There was a good article on the Xamarin Blog about this a while ago.

  • JustJust DKMember ✭✭
    edited October 2017
    public enum Themes
        {
            //
            // Summary:
            //     Dark Theme.
            Dark = 0,
            //
            // Summary:
            //     Light Theme.
            Light = 1        
        }
    
        public void ChangeTheme(Themes themeChoice)
        {
            var currRess = App.Current.Resources;
    
            switch(themeChoice)
            {
                case Themes.Dark:
                    var newTheme = typeof(DarkThemeResources);
                    currRess.MergedWith = newTheme;
                    break;
                case Themes.Light:
                    newTheme = typeof(LightThemeResources);
                    currRess.MergedWith = newTheme;
                    break;
            }
        }
    

    This is how i current switch themes in Xamarin.Forms

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    > @Just said:
    > public enum Themes { // // Summary: // Dark Theme. Dark = 0, // // Summary: // Light Theme. Light = 1 } public void ChangeTheme(Themes themeChoice) { var currRess = App.Current.Resources; switch(themeChoice) { case Themes.Dark: var newTheme = typeof(DarkThemeResources); currRess.MergedWith = newTheme; break; case Themes.Light: newTheme = typeof(LightThemeResources); currRess.MergedWith = newTheme; break; } }
    >
    > This is how i current switch themes in Xamarin.Forms

    Can you post your r sources?
  • BillyMartinBillyMartin USMember ✭✭✭

    @AlessandroCaliaro How can I include my background image in the application Resource Dictionary. I know how to do colors, but how would I do a string?

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭
    edited February 2

    I hope this will work Link Sample
    ''void OnThemeButtonClicked (object sender, EventArgs e)
    {
    var toolbarItem = (ToolbarItem)sender;
    if (isDarkTheme) {
    toolbarItem.Text = "Light";

                App.Current.Resources ["backgroundColor"] = Color.White;
                App.Current.Resources ["textColor"] = Color.Black;
            } else {
                toolbarItem.Text = "Dark";
    
                App.Current.Resources ["backgroundColor"] = Color.FromHex ("33302E");
                App.Current.Resources ["textColor"] = Color.White;
            }
    
            isDarkTheme = !isDarkTheme;
        }'
    
Sign In or Register to comment.