I'm starting to go crazy. I've been chasing this issue for about a day now and I can't figure out how to easily resolve it. So I figured I'd drive you all crazy with this too (hopefully you have better luck than me).
Note: A link to a zipped version of the project is provided below in the Edit section.
I have a C# project structure that resembles the following:
├ Shared Solution folder (containing shared projects)
├──── Shared.UI Shared.UI - Shared Project
├──────── Style.xaml Resource Dictionary xaml file
├ MyProject Solution folder (containing related projects)
├──── MyProject.UI MyProject.UI - WPF App Project
├──────── MainWindow.xaml The xaml file using Style.xaml
The solution contains multiple projects organized by purpose, thus MyProject
contains multiple projects. Project structure must be maintained as is. Also note that the solution folders also exist in explorer (not that it should make a difference).
The names of my projects are Shared.UI
and MyProject.UI
explicitly. I have a reference to Shared.UI in the MyProject.UI project.
Note: The shared project Shared.UI must remain a shared project. We have classes that use ifdef
for platform-specific code (which is not available in PCL)
For testing purposes, I've been using an empty Resource Dictionary as follows.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Shared.UI">
</ResourceDictionary>
<Window x:Class="MyProject.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject.UI"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyProject.UI;component/Shared.UI/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
Everything displays correctly in Blend/Visual Studio, no issues occur and everything is properly linked. When you debug the solution (click ), however, you'll get the following exception:
System.Windows.Markup.XamlParseException occurred HResult=0x80131501 Message='Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '12' and line position '6'.
Source= StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at MyProject.UI.MainWindow.InitializeComponent() in C:\Users\ctoscher\source\repos\Test\MyProject\MyProject.UI\MainWindow.xaml:line 1Inner Exception 1: IOException: Cannot locate resource 'shared.ui/style.xaml'.
The important part is IOException: Cannot locate resource 'shared.ui/style.xaml'.
To replicate this issue, you'll need to open MainWindow.xaml in Blend or Visual Studio. Once in the IDE, click on the ResourceDictionary line that contains the Source
attribute (the line that follows)
<ResourceDictionary Source="/MyProject;component/Shared.UI/Styles.xaml"/>
In the Properties panel, set the Source to Styles.xaml
(this should already be selected since my code above sets this attribute).
The Source URI is set to /MyProject.UI;component/Shared.UI/Style.xaml
. If you remove /Shared.UI
from the string, such that you're left with /MyProject.UI;component/Style.xaml
, and run it, it works!
I've also attempted to change the Build Action for Styles.xaml
to every other action; no other method seems to work "out of the box" (without the aforementioned modification).
ResourceDictionary
with its Source
attribute be properly linked/generated in Blend/Visual Studio so as to not create these issues in the first place? (I understand my workaround is allowing my application to run, but how can I fix this annoyance so that my team and I don't have to edit the link every single time?)I appreciate those of you who actually took the time to read all this. I know this is a long post, however, without creating a zip of the solution, this is as much pertinent information as I can give you.
I realize posting files are usually frowned upon, but I feel it may help with the resolution of this question. Here is a link to the project (zipped).
As per @Yuri (in the comments below/chat transcript):
Here is my thoughts. When you open xaml in Blend it sees SharedUI project and assumes it is a DLL as it doesn't see MyProjectUI. Because SharedUI is a shared project all files are linked directly to ProjectUI, so when you remove SharedUI from path it works.
The issue likely exists as a bug in Blend/Visual Studio not adjusting the method for link generation for shared projects, but if anyone has any suggestions as to how this can be resolved, it would be greatly appreciated.
User contributions licensed under CC BY-SA 3.0