Bug 60788 - 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.
Summary: Can't include key'ed ResourceDictionary's with implicit styles in the Applica...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.5.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-11-21 16:04 UTC by Jim Albert
Modified: 2018-02-24 21:32 UTC (History)
5 users (show)

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 (129.94 KB, application/x-zip-compressed)
2017-11-21 16:04 UTC, Jim Albert
Details
Screenshot (3.09 KB, image/png)
2017-11-21 16:04 UTC, Jim Albert
Details
Exception and Stack Trace (4.08 KB, text/plain)
2017-11-21 16:05 UTC, Jim Albert
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

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.