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:
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:
using System.Windows;
namespace WpfCustomControl {
public partial class SomeWindow : Window {
public SomeWindow() {
InitializeComponent();
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:
<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>
<!--
</Grid>
</Window>
In this way,
CustomControl
cannot be used. I can be used only if you add a namespace:
<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