My cross-platform App class (extends Application) contains a ResourceDictionary that I am attempting to merge with a "branding" dictionary. I cannot access any of the merged dictionary entries in my App constructor prior to creating my root page. In my App.xaml
, I attempt to merge with a BrandTheme.xaml
in the usual manner, first using the MergedWith idiom,
<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="...xamarin.com/schemas/2014/forms" xmlns:x="...schemas.microsoft.com/winfx/2009/xaml" xmlns:theme="clr-namespace:Modern.Designer.Mobile" x:Class="Modern.Designer.Mobile.App"> <Application.Resources> <ResourceDictionary MergedWith="theme:BrandTheme"/> </Application.Resources> </Application>
(Note, I can't post links yet since I am new here.)
I also tried it using the MergedDictionaries idiom,
<Application xmlns="...xamarin.com/schemas/2014/forms" xmlns:x="...schemas.microsoft.com/winfx/2009/xaml" xmlns:theme="clr-namespace:Modern.Designer.Mobile" x:Class="Modern.Designer.Mobile.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <theme:BrandTheme /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
My App constructor is currently simple, having recently been created from its Visual Studio project template,
public partial class App : Application { protected readonly NavigationPage TheNavigationPage; public App() { this.InitializeComponent(); var myLandingPage = new LandingPage(); this.TheNavigationPage = new NavigationPage(myLandingPage); var myResourceDictionary = Current.Resources; this.MainPage = this.TheNavigationPage; } }
When I examine the myResourceDictionary
variable, I see that its myResourceDictionary.MergedDictionaries
property is populated with the ResourceDictionary from BrandTheme.xaml
, which contains nothing but some keyed Color declarations at this time, but does not contain those Color declarations! In fact, both myResourceDictionary.MergedDictionaries[0]
and myResourceDictionary
contains zero items in its collection.
So, while the MergedDictionaries idiom does in fact seem to merge dictionaries, within the App constructor the matching XAML is apparently not yet parsed and the dictionaries loaded. Is this by design? If so, by when should the resource dictionaries be populated and ready for use? If not, what am I failing to do to trigger the XAML processing? Otherwise, is there a bug?
BrandTheme.xaml
is declared as an "Embedded Resource" and its custom tool is "MSBuild:UpdateDesignTimeXaml"- My version of Visual Studio Community 2017 is the latest, 15.6.1
- Xamarin 4.9.0.749
- Xamarin Designer 4.10.58
- I am running the UWP project for this test. I have yet to test for either Android or iOS.
- I am running under the latest Windows 10 release, 10.0.16299.248
LandingPage.xaml
is only the intial boilerplate at this time, freshly created from the Visual Studio Xamarin page template. It contains a ContentPage and a simple text label.- When I embed my Color declarations directly in the high-level
App.xaml
they appear to my App constructor just fine.