Bug 56030 - Properties with DynamicResource are incorrectly resolved when MergedWith is used (resource declaration order affects resolution)
Summary: Properties with DynamicResource are incorrectly resolved when MergedWith is u...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2017-05-05 11:49 UTC by Leo
Modified: 2017-06-03 15:58 UTC (History)
6 users (show)

Tags: dynamicresource mergedwith style ac
Is this bug a regression?: ---
Last known good build:


Attachments
Solution to reproduce the issue (60.81 KB, application/zip)
2017-05-05 11:49 UTC, Leo
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 Leo 2017-05-05 11:49:04 UTC
Created attachment 21974 [details]
Solution to reproduce the issue

The attached solution contains consists of:

App.xaml
========

<?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="DynamicResourceMergedWith.App"
			 xmlns:local="clr-namespace:DynamicResourceMergedWith.Themes;assembly=DynamicResourceMergedWith">
	<Application.Resources>
		<ResourceDictionary MergedWith="local:ColorsResources">

			<Style TargetType="Label">
				<Setter Property="TextColor" Value="{DynamicResource DefaultColor}" />
			</Style>

		</ResourceDictionary>
	</Application.Resources>
</Application>

ColorsResource.xaml
===================

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary 
	xmlns="http://xamarin.com/schemas/2014/forms" 
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
	x:Class="DynamicResourceMergedWith.Themes.ColorsResources">

	<Color x:Key="TestColor">#00FF00</Color> -> THIS IS GREEN

	<Color x:Key="DefaultColor">#FF0000</Color> -> THIS IS RED
</ResourceDictionary>


TestPage.xaml
=============

<?xml version="1.0" encoding="utf-8"?>
<ContentPage 
		xmlns="http://xamarin.com/schemas/2014/forms" 
		xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
		xmlns:local="clr-namespace:DynamicResourceMergedWith" 
		x:Class="DynamicResourceMergedWith.TestPage">

	<Label Text="Green = :)" HorizontalOptions="Center" VerticalOptions="Center" TextColor="{DynamicResource TestColor}" />
	
</ContentPage>


Bug: If I execute the solution as it is, the label is displayed in Red. The expected color is Green since the property is explicitly set.

Other observations:
- If I change the order of the colors in the ColorsResource.xaml, i.e. first define the DefaultColor then the TestColor, the label is displayed in green.
- If I use StaticResource instead of DynamicResource in the TestPage, for the TextColor, green is used independently of the order of the colors
- If I remove the implicit style, keeping the dynamic resource green is shown
Comment 1 Paul DiPietro [MSFT] 2017-05-05 16:41:51 UTC
Thanks for the reproduction, setting to confirmed until this can be investigated further.
Comment 2 Chris King 2017-05-10 18:52:57 UTC
Opened bug https://bugzilla.xamarin.com/show_bug.cgi?id=56189 to report difficulty debugging into XF while investigating this bug; falling back to Debug.WriteLine...
Comment 4 Drew Lederman 2017-05-25 22:39:10 UTC
I'm seeing similar behavior regardless of using implicit/explicit styles.

I have some default resources defined like this:

<ResourceDictionary x:Class="DefaultResources">
  <Color x:Key="TextColor">#000000</Color>
</ResourceDictionary>

Then I have another set of resources that should override the defaults:

<ResourceDictionary x:Class="CustomResources">
  <Color x:Key="TextColor">#FF0000</Color>
</ResourceDictionary>

I'm merging them at runtime like this:

Xamarin.Forms.Application.Current.Resources = new DefaultResources {
  MergedWith = typeof(CustomResources)
};

When I apply the color to a view using DynamicResource, I get the correct color, i.e., the overridden color, #FF0000.

However, if I pull the color directly from the resource dictionary (by either the indexer or TryGetValue), I get the default color, #000000.
Comment 5 Rui Marinho 2017-05-26 11:34:01 UTC
Should be fixed on 2.36-pre1
Comment 6 Leo 2017-06-03 15:58:21 UTC
When will 2.3.6-pre1 be available? Tks