1

I'm using Visual Studio 2017 (15.9.7) Targeting .Net Framework 4.7.1.
I ran into an issue of not having transitive references copied to my output folder.
The code compiles without issues.
I have investigated the issue and found the root cause to be a missing reference in the compiled assembly.

I have distilled down the problem, and here's what I found:

I have 2 class libraries:

  • ClassLibrary1 has a xaml UserControl that references a BitmapImage from ClassLibrary2:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:media="clr-namespace:ClassLibrary2;assembly=ClassLibrary2"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Image Source="{x:Static media:MediaResourceImages.Autofocus_16}" />
</UserControl>
  • ClassLibrary2 has a png resource file compiled within it, and a public static readonly BitmapImage field in a public static class that's created from the png:
using System;
using System.Windows.Media.Imaging;

namespace ClassLibrary2
{
    public static class MediaResourceUris
    {
        public const string Autofocus_16 = "pack://application:,,,/ClassLibrary2;component/Resources/Autofocus_16.png";
    }

    public static class MediaResourceImages
    {
        public static readonly BitmapImage Autofocus_16 = new BitmapImage(new Uri(MediaResourceUris.Autofocus_16));
    }
}

After compiling successfully, ILSpy shows that ClassLibrary1 has no reference to ClassLibrary2, even though the baml references a field from ClassLibrary2:

ILSpy screenshot

Minimal solution to reproduce via GitHub

Is this normal?
Is this a bug in the compiler?
There's obviously a runtime dependency of ClassLibary1 on ClassLibary2, so why isn't a reference compiled into the metadata of ClassLibrary1?

  • Compared to WindowsForms - where the designer was at least work on part of the partial class - the XAML part is notoriously buggy and poorly integrated. Indeed it took use years to get halfway decent debugger support for any XAML caused exeptions. Before that, we basically had to guestimate wich XAML line might have caused an issue. Nevermind that on XAML errors, the thing would still compile - using the last good XAML instead! So it is entirely possible this is an edge case, where it is not working properly and translating the XAML reference need to a Project reference need. – Christopher Oct 22 '19 at 14:54
  • Others seem to have similar problems w.r.t. XAML and references: https://stackoverflow.com/questions/15816769/dependent-dll-is-not-getting-copied-to-the-build-output-folder-in-visual-studio#comment54055330_24828522 – zastrowm Oct 22 '19 at 14:58

1 Answers1

0

This is a known XAML issue. The compiler doesn't copy references that are only used in the XAML markup.

The workaround is easy. Set the Copy Local property for the ClassLibrary2 assembly to True in the ClassLibrary1 library project, or reference a type in ClassLibrary2 from ClassLibrary1 programmatically in C#.

mm8
  • 163,881
  • 10
  • 57
  • 88