Adam Haile
Adam Haile

Reputation: 31329

WPF User Control cannot find assembly during Design Time

I have a series of WPF UserControls in a single Class Library. There are multiple levels to the controls and the top most level will use multiple lower level controls. Some of the low level controls import classes from other external DLLs and then use those classes in properties of the control. The problem is that when I try to drop one of those lower level controls into a higher level control they error when trying to render in the designer, stating that it cannot find the class assemblies used for the properties. They will, however work fine if dropped onto the main window of an executable and actually run perfectly in runtime mode, just not in the designer. Not the end of the world... but a royal pain.

As a quick example:

using MyClassLibrary;

public partial class MyControl : UserControl
{
    public MyControl {}

    public MyClass ClassInstance { get; set; }
}

The above code would render fine in the designer itself, just not when I try to use that control inside of another User control.

I also know the same problem can arise from external classes used in the constructor, OnLoaded, etc (anything that's run by the designer) but have already fixed those by checking if it's in Design mode and disabling that code from running. It's just that I cannot figure out how to have it not process the control properties.

Any ideas?

Upvotes: 1

Views: 1964

Answers (3)

Healer
Healer

Reputation: 290

xmlns:usercontrols="clr-namespace:Company.Project.OtherAssembly.UserControls;assembly=Company.Project.OtherAssembly"

Do you add assembly attribute?

Upvotes: 0

Matthew Lowe
Matthew Lowe

Reputation: 1400

I encountered a similar problem, and worked around it by having the constructor actually call into a second function that called the code in the library:

    public WorkspacePanel()
    {
        InitializeComponent();
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            runtimeConstructor();
        }
    }

    private void runtimeConstructor()
    {
        MyLibraryClass foo = new MyLibraryClass();
    }

Somehow this additional level of indirection fooled it.

But I ran into an additional problem when I started trying to reference my library in the form XAML: the designer just couldn't find it. It turns out that the designer can't find unmanaged libraries unless they're on your system path. So if this is the same problem, adding the output directory to your PATH environment variable may fix it.

Upvotes: 1

Viv
Viv

Reputation: 2595

Did you reference the class in the Xaml? Example:

xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"

Upvotes: 0

Related Questions