Issue with MergedDictionaries after updating to Xamarin.Forms 5

Panag An 21 Reputation points
2021-03-12T00:04:56.16+00:00

Hello everyone, I'm working on a project using Xamarin.Forms and in order to keep resources differentiated for different devices, I've created two xaml files, containing the same keys; one for phone devices and one for tablet devices.

During the app's initialization Application.OnStart(), I used MergedDictionaries to incorporate these resources:

var resourceDictionary = new ResourceDictionary();
var source = new Uri(resourcesPath, UriKind.Relative);
resourceDictionary.SetAndLoadSource(source, resourcesPath, this.GetType().Assembly, null);
Current.Resources.MergedDictionaries.Add(resourceDictionary);

The check, leading to the correct resource file is omitted here.

I was using Xamarin.Forms 4.8.0.1821 and it was working fine, but after updating to Xamarin.Forms 5.0.0.2012 , it stopped working, throwing a NullReferenceException at startup, with the following stack trace:

{System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.VisualElement.OnParentResourcesChanged (System.Collections.Generic.IEnumerable 1[T] values) [0x0008e] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:964   
at Xamarin.Forms.Element.OnParentResourcesChanged (System.Object sender, Xamarin.Forms.Internals.ResourcesChangedEventArgs e) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:402   
at Xamarin.Forms.Element.OnResourcesChanged (System.Collections.Generic.IEnumerable 1[T] values) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:433   
at Xamarin.Forms.Element.OnResourcesChanged (System.Object sender, Xamarin.Forms.Internals.ResourcesChangedEventArgs e) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:424   
at Xamarin.Forms.ResourceDictionary.OnValuesChanged (System.Collections.Generic.KeyValuePair 2[System.String,System.Object][] values) [0x00008] in D:\a\1\s\Xamarin.Forms.Core\ResourceDictionary.cs:340   
at Xamarin.Forms.ResourceDictionary.MergedDictionaries_CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000ad] in D:\a\1\s\Xamarin.Forms.Core\ResourceDictionary.cs:133   
at System.Collections.ObjectModel.ObservableCollection 1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263   
at System.Collections.ObjectModel.ObservableCollection 1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:338   
at System.Collections.ObjectModel.ObservableCollection 1[T].InsertItem (System.Int32 index, T item) [0x0001a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:196   
at System.Collections.ObjectModel.Collection 1[T].Add (T item) [0x00020] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/ObjectModel/Collection.cs:71

The crash occurs for both iOS and Android.
Does anyone have any idea why this is happening? I went over the CHANGELOG on the Xamarin.Forms 5.0 update but I didn't find anything helpful. Any help is appreciated!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,006 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,876 Reputation points
    2021-03-12T07:52:51.367+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    but after updating to Xamarin.Forms 5.0.0.2012 , it stopped working, throwing a NullReferenceException at startup

    I created a basic demo to test the function on Xamarin.Forms 5.0.0.2012, it works fine. Try to create a custom ResourceDictionary.xaml and then MergedDictionaries.Add() method to add the resourceDictionary directly. Check the code:

       //page.xaml  
       <Label Text="testing for dictionary" TextColor="{DynamicResource theBackgroundColor}" />  
         
       //page.xaml.cs  
       App.Current.Resources.MergedDictionaries.Add(new CustomResourceDictionary());  
    

    Custom ResourceDictionary:

       //.xaml  
       <ResourceDictionary ...  
           x:Class="ResourceDictionaryDemo.CustomResourceDictionary">  
           <Color x:Key="theBackgroundColor">Red</Color>  
       </ResourceDictionary>  
       //.cs  
       public partial class CustomResourceDictionary : ResourceDictionary  
       {  
           public CustomResourceDictionary()  
           {  
               InitializeComponent();  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful