Yea, that’s a long title to the article. However, the information in this article is super useful when you have to cross the same bridge because there is no clear way to debug. Here’s what’s going on.
I am making a UWP app in Visual Studio. I want to split out some of my code from my app into a reusable common piece. To do this, I create a Common “Class Library”. I add a bunch of classes, like usual. Then reference the Common assembly in my app project. All is good up to this point.
Now, I want to put some XAML ResourceDictionaries in the Common assembly. Here’s where it is not so obvious. I go to my app’s resources and have something like this:
<ResourceDictionary Source="Dictionaries/BaseDictionary.xaml" />
That was good when the ResourceDictionary lived with the app project. But now that it lives in an external assembly, I thought I had to do something like this:
<ResourceDictionary Source=”Common;component/Dictionaries/BaseDictionary.xaml” />
Or like this:
<ResourceDictionary Source=”/pack://application:,,,/Common;/Dictionaries/BaseDictionary.xaml” />
Neither works and causes a crash during runtime. Instead, the answer was to do something like this:
<ResourceDictionary Source=”ms-appx:///Common/Dictionaries/BaseDictionary.xaml” />
The lessons here are two-fold:
- Microsoft is constantly changing the way the referencing rules work. Some of my attempts were WPF (Windows 7) and Metro (Windows 8).
- Another piece I happened to confirm on my “journey of research” is that the compile action for a ResourceDictionary should be set to “Page”.
Props to this SO question, this SO question, and this SO question (among maybe 50-100 other pages) which finally led to the correct answer. I really wish the Microsoft documentation was easily searchable through Google and that everyone made clear distinctions between the varying forms of WPF/XAML.