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: IN_PROGRESS
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: 2017-12-14 06:08 UTC (History)
4 users (show)

See Also:
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

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.

Note You need to log in before you can comment on or make changes to this bug.