Nick
Nick

Reputation: 2265

WPF: XAML Custom Namespace

Okay so I have a Window in WPF. I add the following line inside of it:

xmlns:controls="clr-namespace:mCubed.Controls"

This compiles and runs just fine, but the Visual Studio designer gives me this error:

Could not load file or assembly 'mCubed, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

When I remove this line from the Window, it compiles and runs just fine and the Visual Studio designer works like a charm!

I'm confused as to why that one line breaks the designer? This occurs REGARDLESS if I have put the following line in the body of the XAML document.

<controls:MyControl/>

All my .cs files are in the same VS project. I have a mCubed namespace which contains my cleverly named mCubedWindow class. I have all my controls classes defined in the mCubed.Controls namespace. Do NOT tell me this is an assembly problem, ALL MY FILES ARE IN THE SAME VS PROJECT!

Upvotes: 2

Views: 6884

Answers (4)

Kent Boogaart
Kent Boogaart

Reputation: 178810

Not an assembly problem, just a designer problem. The VS WPF designer in 2008 is primitive at best - completely useless IMHO. I turn it off completely and use the XML editor instead. Hopefully things will improve drastically in 2010.

Upvotes: 5

Danny Varod
Danny Varod

Reputation: 18118

Is the XAML loose (Build action: None, No code behind) or compiled (Build action: Page, Can have code behind)?

If the XAML is loose or if MyControl is in a different assembly you must specify which assembly MyControl is in, like Daniel Pratt stated:

xmlns:controls="clr-namespace:mCubed.Controls;assembly=mCubed"

Make sure the assembly mCubed and its dependencies (references) are copied to your output directory. If they are not, then add mCubed as a reference to the start-up project.

Upvotes: 0

Daniel Earwicker
Daniel Earwicker

Reputation: 116744

That's a bit weird. I've developed several projects that do exactly that. Here's a quick dummy project, all in one .exe:

First, a UserControl with a couple of buttons:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid Width="30">
        <Button HorizontalAlignment="Left">A</Button>
        <Button HorizontalAlignment="Right">B</Button>
    </Grid>
</UserControl>

Now the main window, with my control added to it:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <p:UserControl1/>
    </Grid>
</Window>

No error messages anywhere.

Upvotes: 1

Daniel Pratt
Daniel Pratt

Reputation: 12077

Is MyControl in the same assembly as the window? If it isn't, you need to include the assembly name in the declaration:

xmlns:controls="clr-namespace:mCubed.Controls;assembly=mCubed"

Upvotes: 2

Related Questions