问题描述
我正在使用VS2013并设置了一个标签控件,每个标签项都包含 UserControl
。当我在XAML中添加它时,Intellisense会看到它,但编译器坚持在名称空间中不存在用户控件,但应用程序编译并按预期运行。
有没有人知道我需要做些什么来解决这个问题?
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?
推荐答案
WTF你在说什么?为什么你甚至会想到我没有做出选择? Intellisense看到它 - 我选择它 - 代码没有f * ckin编译(或者我得到一个错误并且它以任何方式编译)。那里的逻辑在哪里?
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?
我想我可以很容易地解释其中的可能逻辑,但当然我不能对微软的决定负责;我只能解释为什么我认为这很合乎逻辑。首先让我告诉你我的结论摘要:默认情况下,定义窗口的命名空间在XAML中可见,因为它不需要定义同一个窗口。听起来像是一个悖论,但事实上并没有什么美妙的东西。
但首先,让我们确保理解你所做的一切。让我们试着回溯你的步骤。
首先,我将创建一些控制类。它不一定是用户控件;也许最简单的合适定义是:
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();
// just to demostrate that a custom control is accessible:
CustomControl someControl = new CustomControl();
//...
}
}
}
这个窗口是通过XAML创建的,但我从C#部分开始。另一个C#部分是自动生成的,可以在bin子目录中找到;我不会表现出来。首先,我通过在上面显示的代码示例中添加一些代码来证明可以访问该控件。当然,它汇编了。现在,让我们尝试向XAML添加此类型的另一个控件:
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>
<!--
<CustomControl /> here won't be shown in Intellisense
and would cause the build error; it is not visible
-->
</Grid>
</Window>
这样,无法使用 CustomControl
。我只有在添加名称空间时才能使用:
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>
这不是你的经历。我觉得你觉得这不合逻辑。
如果我对你的困惑的假设是正确的(或者即使没有),我可以解释为什么它看起来很合乎逻辑。问题是:除了某些窗口(或用户控件)之外,这种样本永远不会定义任何 UIElement
。在所有情况下,它都不能使用自己。这样,它的名称空间与要使用的控件的名称空间无关。这就是你必须明确添加命名空间的原因。这不合逻辑吗?
另一种解释是来自XML点观点。注意大多数典型的XAML元素(例如 Grid
, DockPanel
, Button
,标签
...)没有名称空间前缀。这是什么意思?这意味着它们属于默认命名空间,对于XAML, []。
现在注意:只能有一个默认命名空间。
如果你不得不写一些类似< wpf:Button>
或<$的话,我认为你会对XAML更不满意C $ C>< WPF:网格和GT; 。因此,决定将所有其他元素(自定义元素)放在单独的非默认(前缀)命名空间中。包括窗口的自己的命名空间。我认为这是合乎逻辑的。
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?
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.
这篇关于WPF编译器Insist对象不在命名空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!