我有一个非常简单的用户控件,我试图在XAML中实例化它。我发现,当我对命名空间有些过分热情时,我遇到了x:Name的问题。用户控件中的Overspecified命名空间失败WPF构建
这里是我的用户:
<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>
这里是代码隐藏的用户控件:
Namespace UserControlTest
Partial Public Class UserControl1
End Class
End Namespace
现在,请注意,我有我的VB.Net项目设置的根命名空间“UserControlTest”。知道了,看看我的主窗口: 这里是我的主窗口:
<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>
见控制别名如何需要有“UserControlTest.UserControlTest”?这是因为我的项目的根名称空间设置为UserControlTest,并且我还将UserControl的名称空间定义为UserControlTest。如果我不使用UserControl的命名空间,我没有任何麻烦。
但是,因为我已经做到了这一点,构建失败,我应该试着申请一个X:名字到用户控件,如下所示:
<control:UserControl1 x:Name="test"/>
这将构建失败,这个错误:
Type 'UserControlTest.UserControlTest.UserControl1' is not defined.
有人可以解释为什么吗?我是否需要避免将UserControls放入命名空间中,以便我可以给它们使用x:Name values?我想操作我的UserControls从代码隐藏,并没有一个x:Name,我上了小溪。但我不想牺牲命名空间的使用来获得它!
非常感谢。
我的用户控件没有在代码隐藏中定义的名称空间。 你确定有关Foo.Controls的命名吗?我添加了一个Controls文件夹和一个新的UserControl。它没有在代码隐藏中定义的名称空间。我的主窗口XAML有xmlns:control =“clr-namespace:UserControlTest”(即NOT clr-namespace:UserControlTest.Control),并且能够使用control:前缀在XAML中创建新的UserControl。 – TimH 2009-07-09 18:07:55