views:

431

answers:

1

I have a very simple user control, and I'm trying to instantiate it in XAML. I find that when I go a bit overzealous with the namespacing, I run into problems with x:Name.

Here is my UserControl:

<UserControl x:Class="UserControlTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
<Grid>
    <Label Name="Label1">Label</Label>
</Grid>
</UserControl>

Here is the code-behind for the UserControl:

Namespace UserControlTest
Partial Public Class UserControl1

End Class
End Namespace

Now, note that I have the root namespace of my VB.Net project set to "UserControlTest". Knowing that, have a look at my main window: Here is my main window:

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

See how the control alias needs to have "UserControlTest.UserControlTest"? That's because I have the root namespace of my project set to UserControlTest, and I have defined the namespace of the UserControl to be UserControlTest, also. If I don't use a namespace for the UserControl, I don't have any troubles.

However, because I have done this, the build fails should I try to apply an x:Name to the UserControl, as follows:

    <control:UserControl1 x:Name="test"/>

That will fail the build, with this error:

Type 'UserControlTest.UserControlTest.UserControl1' is not defined.

Can anybody explain why? Do I need to avoid putting my UserControls into namespaces just so I can give them x:Name values? I'd like to manipulate my UserControls from code-behind, and without an x:Name, I'm up the creek. But I don't want to sacrifice namespace usage just to get it!

Thanks very much.

A: 

What is the namespace defined as in the code-behind of your user control?

If your project was called Foo and you had a folder called Controls inside that project, any new user control added to that folder would be given the namespace Foo.Controls.

Then in your XAML you can reference it like so:

xmlns:Controls="clr-namespace:Foo.Controls"
...
<Controls:UserControl1 x:Name="uc1"/>

It seems like you have a naming issue.

EDIT:

Here's how I'm doing it in a project of mine.

StatusBar.xaml.cs

namespace Client.Controls.UserControls
{
public partial class StatusBar : UserControl
{ 
...
}
}

StatusBar.xaml

<UserControl x:Class="Client.Controls.UserControls.StatusBar">
</UserControl>

Main.xaml.cs

using Client.Controls.UserControls;

namespace Client
{
public partial class Main : Window
{
...
}
}

Main.xaml

<Window x:Class="Client.Main"
xmlns:UserControls="clr-namespace:Client.Controls.UserControls">
<UserControls:StatusBar x:Name="mainStatusBar" />
</Window>
David
My user control has no namespace defined in the code-behind.Are you sure about the Foo.Controls naming? I added a Controls folder, and a new UserControl within it. It has no namespace defined in the codebehind. My main window XAML has xmlns:control="clr-namespace:UserControlTest" (i.e. NOT clr-namespace:UserControlTest.Control) and is able to create the new UserControl in XAML using the control: prefix.
TimH
Please see my edit. Hopefully it makes sense.
David