5

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.

ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • Shouldn't it read `Source="/Shared.UI;component/Styles.xaml"/>` instead? After all it is **Shared.UI** that contains the style not **MyProject.UI**. Anyway, it's generally convention to place all styling in your **App.xaml** rather than specifying it each time for all views –  Nov 11 '17 at 00:02
  • 2
    **Mind explaining the downvote?** My question fully complies with [Help Center > Asking](https://stackoverflow.com/help/asking). I am not seeking debugging help, this problem can be reproduced, it is not homework, nor is it asking for a book, tool, software library, tutorial or other off-site resource, it's not considered general computing hardware/software, and it's not regarding professional server, networking, or related infrastructure administration. The question is not subjective, I've created a Minimal, Complete, and Verifiable example, the title summarizes the problem... Please explain. – ctwheels Nov 14 '17 at 14:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158985/discussion-between-ctwheels-and-yuri-s). – ctwheels Nov 14 '17 at 17:27
  • It's just an issue in Blend and VS where multiple projects being used aren't mapped correctly in paths for resources in XAML. I've been dealing with this for years... This also happens with image paths etc. It sucks but you just have to manually fix it fro now. If you find a fix let me know also. – Michael Puckett II Nov 15 '17 at 14:58
  • 1
    @MichaelPuckettII I guess there's no light at the end of the tunnel... Thanks for your reply. If I do get an answer for this, I'll be sure to let you know. – ctwheels Nov 15 '17 at 15:02
  • Why sometimes your file is named `Style.xaml` and others `Styles.xaml` Can that be related with the issue? Maybe some reference not properly updated? – alseether Nov 20 '17 at 16:19
  • @alseether those are simply typos in the question. If you download the project from the link you should see that they're all synonymous. – ctwheels Nov 20 '17 at 16:21

0 Answers0