Applying Theme switch in MVVM

I'm new to Xamarin Forms and I' trying to add a theme control to my application

I found this post - https://www.syncfusion.com/forums/147666/applying-theme-to-control

on how to (Applying theme to control), but the problem is that I want to know how to do this in MVVM

When I fallow the sample project in the link above and apply's the code to my project, I get the error

>>> "Severity Code Description Project File Line Suppression State Error CS0234 The type or namespace name 'MergedDictionaries' does not exist in the namespace 'CykelStaden.Resources' (are you missing an assembly reference?)"


This is my ViewModel

public class SettingsViewModel : BaseViewModel {private bool toggleTheme;private bool isLightTheme = true;
public bool ToggleTheme        {            get            {                return toggleTheme;            }            set            {                toggleTheme = value;                OnToggleTheme();            }        }private void OnToggleTheme()        {            if (isLightTheme)            {                ICollection<ResourceDictionary> mergedDictionaries = Resources.MergedDictionaries;                var lightTheme = mergedDictionaries.OfType<Themes.LightTheme>().FirstOrDefault();                if (lightTheme != null)                {                    mergedDictionaries.Remove(lightTheme);                }                mergedDictionaries.Add(new Themes.DarkTheme());                isLightTheme = false;            }            else            {                ICollection<ResourceDictionary> mergedDictionaries =   this.Resources.MergedDictionaries;                var darkTheme = mergedDictionaries.OfType<Themes.DarkTheme>().FirstOrDefault();                if (darkTheme != null)                {                    mergedDictionaries.Remove(darkTheme);                }                mergedDictionaries.Add(new Themes.LightTheme());                isLightTheme = true;            }        }}


How do I fix this please


Attachment: SettingsViewModel_6c53d39f.zip

2 Replies

JK Jeya Kasipandi Syncfusion Team November 8, 2021 12:55 PM UTC

Hi Jonas,

Currently, we are validating on this and will update the details on November 11, 2021.We appreciate your patience until then.

Regards,
Jeya K




RS Ruba Shanmugam Syncfusion Team November 11, 2021 02:17 PM UTC

Hi Jonas Jsk, 
 
Thanks for your patience. We have prepared a sample based on the provided code snippet. Can you get the sample link from below. 
 
 
Please let us know, if you have any other concerns. 
Regards, 
Ruba Shanmugam 


Loader.
Up arrow icon