Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using VS2013 and have setup a tab control with each tab item containing a UserControl. Intellisense sees it when I add it in the XAML, but the compiler is insisting that a user control doesn't exist in the namepace, yet the the app compiles and runs as expected.

Does anyone have any idea what I need to do to fix this?
Posted

The solution was to add the assembly name to the namespace declaration in the xaml - but I'm not sure why that was necessary when the control is in the same freaking assembly as the window that's using it (which also happens to be the application exe).

(Reason for edit - fixed some spelling errors)
 
Share this answer
 
v2
Comments
Abhinav S 21-Jul-14 10:03am    
Yes this is required. If you use a converter (in Silverlight) in the same namespace for example, the reference needs to be added before using it.
Sergey Alexandrovich Kryukov 21-Jul-14 11:57am    
Of course it is required, and it's clear why. The namespace of the UI element using your user control is totally irrelevant. If you think about it, you will see that it does not help to figure out the namespace of the user control. You will understand it if you change the namespace of the user control to the one not identical to the namespace of a using window. Did you get it?
—SA
#realJSOP 21-Jul-14 12:23pm    
No, I don't get it - not at all. If intellisense can find it when I actually add it to the XAML, why doesn't it compile? We're talking about controls here that are nothing more than the template code provided by VS, and I'm not getting errors regarding those controls. Further, they are in the same assembly AND namespace as the main window whch is actually hosting the control. The entire soution is essentially a flat hierarchy with no additional projects or folders (yet).

I had to remove the xaml that uses the controls so I could code other parts of the app.
Sergey Alexandrovich Kryukov 21-Jul-14 12:59pm    
When you put '.' after an object, Intellisense also suggests you something, but if you actually don't make your choice and complete the code, it won't compile. Imagine you have several different user controls with identical name and even interface, but in different namespace? Same thing...
—SA
#realJSOP 21-Jul-14 13:51pm    
WTF are you talking about? Why would you think even for a second that I didn't "make a selection"? Intellisense sees it - I select it - code doesn't f*ckin compile (or I get an error and it compiles any way). Where's the logic in that?
Try cleaning the solution and then do a build.
 
Share this answer
 
Comments
#realJSOP 21-Jul-14 9:11am    
If only it were that simple - that's the first thing I tried. I fixed it though, and posted my solution.
#realJSOP 21-Jul-14 9:20am    
Well, that solved the initial problem, but now it won't compile at all.

The tag 'CtrlTabGallery' does not exist in XML namespace 'clr-namespace:ContentManager;assembly=ContentManager'.

I hate WPF...
John Simmons wrote:
WTF are you talking about? Why would you think even for a second that I didn't "make a selection"? Intellisense sees it - I select it - code doesn't f*ckin compile (or I get an error and it compiles any way). Where's the logic in that?
I think I can easily explain the possible logic in that, but of course I cannot be responsible for Microsoft decisions; I can only explain why I consider it quite logical. Let me first tell you the summary of my conclusion: the namespace where the window is defined is not by default visible in the XAML defining the same window just because it is non needed. Sounds like a paradox, but in fact there is nothing wonderful in it.

But first, let's make sure I understand what you have done. Let's try to retrace your steps.

First, I'll create some control class. It does not have to be a user control; perhaps the simplest suitable definition would be this:
C#
using System.Windows.Controls;

namespace WpfCustomControl {
    class CustomControl : Control {
    }
}

Now, let's create a window and see how it can use the control defined above. I made sure the window and control are in the same namespace:
C#
using System.Windows;

namespace WpfCustomControl {
    public partial class SomeWindow : Window {
        public SomeWindow() {
            InitializeComponent();
            // just to demostrate that a custom control is accessible:
            CustomControl someControl = new CustomControl();
            //...
        }
    }
}
This windows is created via XAML, but I started with the C# part. Another C# part is auto-generated and can be found in the "bin" sub-directory; I won't show it. First of all, I demonstrated that the control is accessible by adding some code with it in the code sample shown above. It compiles, of course. Now, let's try to add another control of this type to XAML:
HTML
<Window x:Class="WpfCustomControl.SomeWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SomeWindow" Height="300" Width="300">
    <Grid>
        <!--
		<CustomControl /> here won't be shown in Intellisense
		and would cause the build error; it is not visible 
	-->
    </Grid>
</Window>

In this way, CustomControl cannot be used. I can be used only if you add a namespace:
HTML
<Window x:Class="WpfCustomControl.SomeWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:internal="clr-namespace:WpfCustomControl"
    Title="SomeWindow" Height="300" Width="300">
    <Grid>
	<internal:CustomControl/>
    </Grid>
</Window>
Isn't it what you have experiences. I think you find this illogical.

If my assumptions about your confusions are correct (or even if not), I can explain why it looks quite logical. The thing is: this kind of sample never define any UIElement except some window (or user control). In all cases, it cannot use itself. This way, the namespace of it is irrelevant to the namespaces of the controls to be used. That's why you have to add a namespace explicitly. Isn't it quite logical?

[EDIT]

Another explanation would be from the XML point of view. Pay attention that most typical XAML elements (such as Grid, DockPanel, Button, Label…) come without namespace prefix. What does it mean? It means that they come under the default namespace, in case of XAML, "http://schemas.microsoft.com/winfx/2006/xaml/presentation[^]".

Now attention: there can be only one default namespace.

I think you would be more unhappy with XAML if you had to write something like <wpf:Button> or <wpf:Grid>. So, the decision has been made to put all other elements, the "custom" ones, in separate non-default (prefixed) namespaces. Including the window's "own" namespace. I think this is quite logical.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900