![](/img/trans.png)
[英]Why is XAML(WPF) not searching in my XML namespace definitions for the controls?
[英]WPF XAML merge default namespace definitions
在WPF應用程序中,您可以創建XmlnsDefinitions來將多個名稱空間從另一個程序集中映射到一個引用中。
您可以利用它來將自己的名稱空間映射到Microsoft默認的XmlnsDefinition,例如:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "MyLibrary.MyControls")]
這樣,您甚至不需要在XAML文件中添加對名稱空間的引用,因為它們已經映射到默認的“ xmlns”。
因此,如果我有一個名為“ MyControl”的控件,則可以這樣使用它(沒有任何名稱空間或前綴):
<MyControl />
我的問題是 :我可以將默認的Microsoft命名空間合並為一個嗎?
例如:我想通過將“ xmlns:x”命名空間合並到“ xmlns”中來擺脫它。 我必須將“ http://schemas.microsoft.com/winfx/2006/xaml ”中的所有命名空間引用為“ http://schemas.microsoft.com/winfx/2006/xaml/presentation ”。
像這樣:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System...")]
...
所以我可以打開它:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
變成這個:
<Window Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>
您無法覆蓋現有名稱空間映射,因為它們已經存在於標准組件中,但是您可以嘗試將所有.NET名稱空間合並到您自己的XML名稱空間中。 像這樣:
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Controls")]
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Documents")]
[assembly: XmlnsDefinition("urn:foobar", "System.Windows.Shapes")]
// ...
[assembly: XmlnsDefinition("urn:foobar", "FoobarLibrary.Controls")]
// ...
可能有效(我沒有嘗試過)。 您的XAML如下所示:
<Window x:Class="FoobarProject.MainWindow"
xmlns="urn:foobar"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
請注意,您無法擺脫x
名稱空間:
它沒有映射到.NET命名空間,它是純XML命名空間,並且是XAML語言的一部分。
這樣做會導致沖突( x:Name
與Name
)。
說到沖突,像這樣合並名稱空間帶來了麻煩。 您可能會遇到名稱沖突,這些名稱空間旨在解決這些沖突。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.