Assembly only used in XAML is not copied to bin folder
Asked Answered
T

2

11

In WPF project, references only used in xaml is not copied to bin folder even with CopyLocal set to true, as complained in this post. With MVVM pattern, it is common that 3rd party controls are used with no code-behind references at all.

Inspired by this post, I am using the workaround below. It's easy enough, however, one has to manually maintain the variable list in sync with those actually used in the XAML files across the project, which are subject to constant changes. My question is whether there is a real solution or a better workaround? (I am aware of the idea of using post-build script, which IMHO is less discover-able and more vulnerable to changes.)

internal static class BuildTricker
{
    private static readonly RadTreeView radTreeView;
    static BuildTricker()
    {
        // Set and get once to avoid compiler optimization.
        radTreeView = null;
        if (radTreeView != null)
        {
            throw new InvalidOperationException("This should never happen.");
        }
    }
}
Termination answered 29/4, 2012 at 23:53 Comment(2)
That's RAD/Telerik - if it's in the GAC you could try this - <Private>True</Private>Supplication
It's not specific to RadControls, and <Private> tag is the implementation of CopyLocal property.Termination
V
9

The workaround I use is to make sure the item has a name defined in the XAML. This causes it to create an instance for it in the generated code behind (partial) class. This is enough for the reference following system to detect the reference, and copy in the dependency dll.

e.g.:

<ts:HorizontalToggleSwitch IsChecked="{Binding ShowMyItemsOnly}"
                           CheckedContent="Show My Items Only"
                           UncheckedContent="Show All Items"
                           x:Name="NameRequiredForCopyLocal">
Viola answered 15/7, 2013 at 12:31 Comment(4)
I am using a third party dependency for binding markup, and apparently that does not work...Conchitaconchobar
This fixed my issue with oxyplot. Thank you!Appellee
It looks like this solution doesn't work if the control only occurs in control templates.Derris
Hard to believe this is still an issue with VS2022, but it is.Rab
L
2

This is a known problem with XAML-Code. There is something strange during parsing the XAML-Code because VisualStudio knows, that the corresponding DLL is used. For my case it helped to create an Attribute of any object from the specific DLL in the code behind class or any class in the project. (Workaround)

For example:

// This is only for the build process, to copy target assembly to output.
private RadTreeView _TreeViewForCompiler;
Lacustrine answered 31/3, 2015 at 12:28 Comment(1)
I had to use the same workaround. It's annoying, but it has produced good and reliable results for me.Ligation

© 2022 - 2024 — McMap. All rights reserved.