Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.");
        }
    }
}
like image 703
NS.X. Avatar asked Apr 29 '12 23:04

NS.X.


2 Answers

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">
like image 147
Mark Avatar answered Nov 04 '22 08:11

Mark


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;
like image 34
Michi-2142 Avatar answered Nov 04 '22 09:11

Michi-2142