Why does Blend/Visual Studio always generate ResourceDictionary "Source" Incorrectly?

6

Brief

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.


Project Structure

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)


Code

Style.xaml

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>

MainWindow.xaml

<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>

Issue

Everything displays correctly in Blend/Visual Studio, no issues occur and everything is properly linked. When you debug the solution (click Start), however, you'll get the following exception:

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 1

Inner Exception 1: IOException: Cannot locate resource 'shared.ui/style.xaml'.

The important part is IOException: Cannot locate resource 'shared.ui/style.xaml'.

Replicating the Issue

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).

Solving the Error

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).


Question Time

  • How can 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.


Edit

Solution Download

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).

Updates

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.

c#
xaml
visual-studio-2017
resourcedictionary
blend
asked on Stack Overflow Nov 10, 2017 by ctwheels • edited Nov 14, 2017 by ctwheels

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0