Change theme exception

I try to use dark and light themes in Xamarin Forms, but when I change theme the UI don't change, searching and reading documentation here I found a partial solution, when try to change from dark to light or light to dark the application crash.
I share my error.

SfScheduleMapping.HeaderStyle_PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e)System.
NullReferenceException: Object reference not set to an instance of an object.

Stack traces
  • Syncfusion.SfSchedule.XForms.Droid
    SfScheduleMapping.HeaderStyle_PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e)
  • Xamarin.Forms
    BindableObject.OnPropertyChanged (System.String propertyName) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:229
  • Xamarin.Forms
    Element.OnPropertyChanged (System.String propertyName) D:\a\1\s\Xamarin.Forms.Core\Element.cs:353
  • Xamarin.Forms
    BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:461
  • Xamarin.Forms
    BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397
  • Xamarin.Forms
    BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes) D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:343
  • Xamarin.Forms
    Element.OnResourceChanged (Xamarin.Forms.BindableProperty property, System.Object value) D:\a\1\s\Xamarin.Forms.Core\Element.cs:590
  • Xamarin.Forms
    Element.OnResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:446
  • Xamarin.Forms
    Element.OnParentResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:398
  • Xamarin.Forms
    Element.OnParentResourcesChanged (System.Object sender, Xamarin.Forms.Internals.ResourcesChangedEventArgs e) D:\a\1\s\Xamarin.Forms.Core\Element.cs:393
  • Xamarin.Forms
    Element.OnResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:424
  • Xamarin.Forms
    Element.OnParentResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:398
  • Xamarin.Forms
    VisualElement.OnParentResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:899
  • Xamarin.Forms
    Element.OnParentResourcesChanged (System.Object sender, Xamarin.Forms.Internals.ResourcesChangedEventArgs e) D:\a\1\s\Xamarin.Forms.Core\Element.cs:393
  • Xamarin.Forms
    Element.OnResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:424
  • Xamarin.Forms
    Element.OnParentResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:398
  • Xamarin.Forms
    VisualElement.OnParentResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:899
  • Xamarin.Forms
    Element.OnParentResourcesChanged (System.Object sender, Xamarin.Forms.Internals.ResourcesChangedEventArgs e) D:\a\1\s\Xamarin.Forms.Core\Element.cs:393
  • Xamarin.Forms
    Element.OnResourcesChanged (System.Collections.Generic.IEnumerable`1[T] values) D:\a\1\s\Xamarin.Forms.Core\Element.cs:424
  • Xamarin.Forms
    Element.OnResourcesChanged (System.Object sender, Xamarin.Forms.Internals.ResourcesChangedEventArgs e) D:\a\1\s\Xamarin.Forms.Core\Element.cs:415
  • Xamarin.Forms
    ResourceDictionary.OnValuesChanged (System.Collections.Generic.KeyValuePair`2[System.String,System.Object][] values) D:\a\1\s\Xamarin.Forms.Core\ResourceDictionary.cs:327
  • Xamarin.Forms
    ResourceDictionary.MergedDictionaries_CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) D:\a\1\s\Xamarin.Forms.Core\ResourceDictionary.cs:124
  • System.Collections.ObjectModel
    ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263
  • System.Collections.ObjectModel
    ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:338
  • System.Collections.ObjectModel
    ObservableCollection`1[T].InsertItem (System.Int32 index, T item) /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:196


My code:

 ICollection<ResourceDictionary> mergedDictionaries = this.Resources.MergedDictionaries;
                if (ThemeHelper.CurrentTheme == Enums.Theme.Dark)
                {
                    var darkTheme = mergedDictionaries.OfType<DarkTheme>().FirstOrDefault();
                    if (darkTheme != null) return;

                    var lightTheme = mergedDictionaries.OfType<LightTheme>().FirstOrDefault();
                    if (lightTheme != null)
                    {
                        mergedDictionaries.Remove(lightTheme);
                    }
                    mergedDictionaries.Add(new DarkTheme());
                }
                else if (ThemeHelper.CurrentTheme == Theme.Light)
                {
                    var lightTheme = mergedDictionaries.OfType<LightTheme>().FirstOrDefault();
                    if (lightTheme != null) return;

                    var darkTheme = mergedDictionaries.OfType<DarkTheme>().FirstOrDefault();
                    if (darkTheme != null)
                    {
                        mergedDictionaries.Remove(darkTheme);
                    }
                    mergedDictionaries.Add(new LightTheme());
                }

4 Replies 1 reply marked as answer

SS SaiGanesh Sakthivel Syncfusion Team June 4, 2020 02:34 PM UTC

Hi Hugo, 
 
Thank you for the contacting Syncfusion support. 
 
We have checked the reported query Change theme exception from our end. We would like to inform you that you can achieved your requirement of changing themes at run time by removing the ark themes and add the light themes with of button clicked. Please refer to the following code snippet for your reference. 
 
Code Snippet 
private void Theme_Clicked(object sender, EventArgs e) 
{ 
    ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries; 
    var darkTheme = mergedDictionaries.OfType<DarkTheme>().FirstOrDefault(); 
    var lightTheme = mergedDictionaries.OfType<LightTheme>().FirstOrDefault(); 
    if(darkTheme != null) 
    { 
        mergedDictionaries.Remove(darkTheme); 
        mergedDictionaries.Add(new LightTheme()); 
    } 
    else 
    { 
        mergedDictionaries.Remove(lightTheme); 
        mergedDictionaries.Add(new DarkTheme()); 
    } 
} 
 
 
We have prepared the sample as per your requirement and attached in the following link for your reference. 
 
We hope this helps. Please let us know if you need any further assistance. 
 
Regards, 
SaiGanesh Sakthvel 



HV HUGO VLADIMIR FELIX ANAYA June 9, 2020 06:02 AM UTC

Hi SaiGanesh Sakthivel, thanks for reply me, I tested your sample and work fine! but when I use navigation with Shell control in tabs and moved into tabs and back to page when is the Schedule control and try to change the theme with your code (or mine, I tried some things without a great result) the application crash, the exception is a NullReferenceException and I tried a lot of things, I don't know how to solve this issue, in this moment I don't know if I should change the control with some another.

I hope that you could help me.

Thanks, regards.


SS SaiGanesh Sakthivel Syncfusion Team June 10, 2020 03:25 PM UTC

Hi Hugo, 
 
Thank you for the patience. 
   
We have checked, and we can replicate the reported issue Change theme exception. We have logged issue report for the same, we will fix this issue and include the issue fix our upcoming 2020 Volume 2 SP release which is planned to roll out in the month of July, 2020. We appreciate your patience until then. 
 
You can track the status of this report through the following feedback link, 
  
Note: The provided feedback link is private, you need to login to view this feedback.   
  
Regards,  
SaiGanesh Sakthivel


SS SaiGanesh Sakthivel Syncfusion Team August 19, 2020 11:18 AM UTC

Hi Hugo, 
  
Thank you for the patience, 
   
We have fixed the reported issue “Change theme exception” and included the issue fix in our latest Weekly NuGet release update version 18.2.0.54 which is available for download (nuget.org). 
 
We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you would require any further assistance. 
 
Regards, 
SaiGanesh Sakthivel 


Marked as answer
Loader.
Up arrow icon