MVVMCross Xamarin.Forms - How to load Themes Dynamically

25 views Asked by At

I have a MVVMCross Xamarin Forms Application. I have my themes in separate files i.e. "BlueTheme.xaml", "LightTheme.xaml", 'DarkTheme.Xaml". I have a settings page where user can opt which theme to load. The ThemeManager.cs file is in MyApp.Core. But the App.xaml is in MyApp.UI project. How can I add the theme xaml to merge dictionaries?

I am not able to access "Application.Current.Resources.MergedDictionaries" in Core project as it does not have reference of Xamarin.Forms enter image description here

Any help is really appreciated

public static void ChangeTheme(Constants.Themes theme) { var mergedDictionaries = Application.Current.Resources.MergedDictionaries;//This line does not work in Core project if (mergedDictionaries != null) { mergedDictionaries.Clear();

    //Update local key value with the new theme selected
    Preferences.Set(Constants.SETTING_KEY_THEME, (int)theme);

    switch (theme)
    {
        case Constants.Themes.Dark:
            {
                mergedDictionaries.Add(new DarkTheme());
                break;
            }
        case Constants.Themes.Light:
            {
                mergedDictionaries.Add(new LightTheme());
                break;
            }
        case Constants.Themes.Orange:
            {
                mergedDictionaries.Add(new OrangeTheme());
                break;
            }
        default:
            mergedDictionaries.Add(new BlueTheme());
            break;
    }
}

}

0

There are 0 answers