This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 42264 - MergedWith doesn't work at application level
Summary: MergedWith doesn't work at application level
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-06-29 21:03 UTC by adamhewitt
Modified: 2016-12-06 18:50 UTC (History)
12 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Reproduction project (44.12 KB, application/x-zip-compressed)
2016-06-29 21:03 UTC, adamhewitt
Details
Test program demonstrating issue with StaticResource access to MergedWith ResourceDictionary (61.65 KB, application/zip)
2016-07-22 19:01 UTC, Mike Smith
Details

Description adamhewitt 2016-06-29 21:03:24 UTC
Created attachment 16525 [details]
Reproduction project

Application-level resources aren't found in the MergedWith dictionary. My theory is the StaticResource isn't using TryGetValue, rather is using ContainsKey and indexer access. See:https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Xaml/MarkupExtensions/StaticResourceExtension.cs#L40)


I'm not sure the best fix, but suggestions:
1. Have StaticResource use TryGetValue
2. ContainsKey should also look to _mergedInstance to resolve true/false


Adam Hewitt
Comment 1 Mike Smith 2016-07-22 19:01:13 UTC
Created attachment 16763 [details]
Test program demonstrating issue with StaticResource access to MergedWith ResourceDictionary
Comment 2 Mike Smith 2016-07-22 19:02:47 UTC
=== Overview ===
Attached (attachment 16763 [details]) is another project demonstrating this same problem: attempting to access resources from a MergedWith resource dictionary does not work with the {StaticResource …} syntax.

Note that it does seem to work fine when using {DynamicResource …} for the access.

This project demonstrates creating the resources in XAML, vs. in code for the previous example.

=== Background ===
In the attached program (TestMergedWith4), ResourceDictionaries are defined at three levels:

	1) In a separate PCL (In Demo.Custom.Styles.CustomResources)
	2) In App.xaml (which references the separate PCL CustomResources via MergedWith)
	3) In TestMergedWith4Page  (which also references the separate PCL CustomResources  via MergedWith)

When using DynamicResource to access an explicit resource (i.e., one marked with x:key "<name") it works as expected.  But when attempting the same access using StaticResource, it works in some cases and not others.  

	• If there is no page-level dictionary and the key is only defined in the PCL, it does not seem to work.
	• If there is a page-level dictionary and the key is only defined in the PCL, it does seem to work.
	• If the key is defined in either App.xaml or the page-level dictionary it seems to work.

Implicit resources (i.e., one that is not marked with x:key "<name" and that get applied automatically by control type)  seem to work regardless of where they are defined.

In the attached single XAML page program, there is a StackLayout defined in TestMergedWith4Page.xaml.  It references a Style (TitleBarStyle) that is defined at all three levels (PCL, App.xaml and page-level).  By commenting or uncommenting that at the various levels, all the above scenarios can be tried.
	
=== Steps to Reproduce  ===

	1)  Run the attached program (TestMergedWith4)

=== Expected Behavior ===
The StackLayout should access the Style TestMergedWith4 defined in the PCL (as the same style defined in App.xaml and TestMergedWith4.xaml is commented out) and apply a yellow background to the StackLayout.

=== Actual Behavior ===

The programs throws the error "StaticResource not found for  key TitleBarStyle"

=== Build Information ===
Xamarin Studio Enterprise
Version 6.0.1 (build 9)
Runtime:
	Mono 4.4.1 (mono-4.4.0-branch-c7sr0/4747417) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)
	Package version: 404010000
	
Xamarin.Forms ver 2.3.1.113pre3

Xamarin.Android
Version: 6.1.1.1 (Visual Studio Enterprise)
Android SDK: /Users/MacBookPro/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 25.1.7
SDK Platform Tools Version: 24
SDK Build Tools Version: 23.0.3

Operating System
Mac OS X 10.11.6
Darwin MacBookPro 15.6.0 Darwin Kernel Version 15.6.0
    Thu Jun 23 18:25:34 PDT 2016
    root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64
Comment 3 Mike Smith 2016-07-22 19:04:33 UTC
Correction to the above:
=== Expected Behavior ===
The StackLayout should access the Style TitleBarStyle defined in the PCL (as the same style defined in App.xaml and TestMergedWith4.xaml is commented out) and apply a yellow background to the StackLayout.
Comment 4 Samantha Houts 2016-12-06 18:50:03 UTC
Should be fixed in 2.3.4-pre2. Thanks!

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