我有一個非常簡單的用戶控件,我試圖在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