Bug 60788

Summary: Can't include key'ed ResourceDictionary's with implicit styles in the Application's Resources after upgrading from Xamarin.Forms v2.4.0 to v2.5.0.
Product: Forms Reporter: Jim Albert <jalbert>
Component: FormsAssignee: Stephane Delcroix <stephane.delcroix>
Status: RESOLVED FIXED    
Severity: normal CC: david.ortinau, jas, msiccdev, pedersen, sahou
Priority: Normal    
Version: 2.5.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: resourcedictionary, fr Is this bug a regression?: Yes
Last known good build: 2.4.0.91020
Attachments: Visual Studio 2017 15.4.4 solution with a UWP project that reproduces the issue, as well as a screenshot of the expected output and also the exception details and stack trace
Screenshot
Exception and Stack Trace

Description Jim Albert 2017-11-21 16:04:06 UTC
Created attachment 25776 [details]
Visual Studio 2017 15.4.4 solution with a UWP project that reproduces the issue, as well as a screenshot of the expected output and also the exception details and stack trace

Can't include key'ed ResourceDictionary's with implicit styles in the Application's Resources after upgrading from Xamarin.Forms v2.4.0 to v2.5.0.

Our project uses key'ed ResourceDictionary's with implicit styles at the Application level to share a set of styles across multiple pages and controls.  For example:

<?xml version="1.0" encoding="utf-8" ?>
<Application
  xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  x:Class="App5.App">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary x:Key="RedTextBlueBackground">
        <Style TargetType="StackLayout">
          <Setter Property="BackgroundColor" Value="Blue" />
        </Style>
        <Style TargetType="Label">
          <Setter Property="TextColor" Value="Red" />
        </Style>
      </ResourceDictionary>
      <ResourceDictionary x:Key="BlueTextRedBackground">
        <Style TargetType="StackLayout">
          <Setter Property="BackgroundColor" Value="Red" />
        </Style>
        <Style TargetType="Label">
          <Setter Property="TextColor" Value="Blue" />
        </Style>
      </ResourceDictionary>
    </ResourceDictionary>
  </Application.Resources>
</Application>

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
  xmlns="http://xamarin.com/schemas/2014/forms"
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  x:Class="App5.MainPage">
  <ContentPage.Content>
    <StackLayout Resources="{StaticResource RedTextBlueBackground}">
      <Label
        Text="Welcome to Xamarin.Forms!"
        VerticalOptions="CenterAndExpand" 
        HorizontalOptions="CenterAndExpand" />
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

This worked fine as of Xamarin.Forms v2.4.0.91020, but after upgrading to v2.5.0.91635 we're getting the exception:
A resource with the key 'Xamarin.Forms.StackLayout' is already present in the ResourceDictionary

I've attached a Visual Studio 2017 15.4.4 solution with a UWP project that reproduces the issue, as well as a screenshot of the expected output and also the exception details and stack trace.  I can reproduce the issue on both Android and UWP, and it presumable affects iOS as well.  I tried with both the XamlC Compile and Skip options.
Comment 1 Jim Albert 2017-11-21 16:04:40 UTC
Created attachment 25777 [details]
Screenshot

Screenshot
Comment 2 Jim Albert 2017-11-21 16:05:00 UTC
Created attachment 25778 [details]
Exception and Stack Trace

Exception and Stack Trace
Comment 3 Stephane Delcroix 2017-11-22 08:19:02 UTC
https://github.com/xamarin/Xamarin.Forms/pull/1300
Comment 4 MSiccDev 2017-12-14 06:08:33 UTC
I am running into the same problems. As a workaround with Xamarin Forms 2.5, I was able to create the keyed Resources from the App.xaml code-behind file.

Example:
        private void CreateResourceDictionary()
        {
            if (this.Resources == null)
                this.Resources = new ResourceDictionary();
            if (!this.Resources.ContainsKey("Locator"))
            {
                this.Resources.Add("Locator", ViewModels.ViewModelLocator.Instance);
            }

            if (!this.Resources.ContainsKey("MainAccentColor"))
            {
                this.Resources.Add("MainAccentColor", Color.FromHex("#1e73be"));
            }

            if (!this.Resources.ContainsKey("LightAccentColor"))
            {
                this.Resources.Add("LightAccentColor", Color.FromHex("#61a1f1"));
            }

            if (!this.Resources.ContainsKey("DarkAccentColor"))
            {
                this.Resources.Add("DarkAccentColor", Color.FromHex("#00488d"));
            }

            if (!this.Resources.ContainsKey("MainBackgroundColor"))
            {
                this.Resources.Add("MainBackgroundColor", Color.FromHex("#f4f4f4"));
            }
        }

At least, this does not require additional code changes until this bug is fixed.
Comment 5 Samantha Houts [MSFT] 2018-02-02 21:39:47 UTC
Should be fixed in 2.5.0-sr3. Thanks!
Comment 6 Thomas Hiilbig Pedersen 2018-02-24 21:32:38 UTC
Do you know when that is going into the regular release? I am still seeing this exact issue with Xamarin.Forms version 2.5.0.280555 on both Android and iOS.