Search code examples
wpfxamlmsbuildcopy-local

Assembly only used in XAML is not copied to bin folder


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.");
        }
    }
}

Solution

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