我有点厌倦了在每一个XAML文件中声明了xmlns,不得不使用prefixes我的自定义控件。是否有可能一个CLR命名空间映射到http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation?
我尝试了以下在我的AssemblyInfo.cs:
[总成:XmlnsDefinition(http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation,MyOwnNamespace)
不过这似乎并没有工作。我仍然得到一个编译错误,如:
在'MyCustomControl标签没有在XML命名空间存在'http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation。
注:我的控制是相同的组件内(我有一个程序集)
解决方案不幸的是,你不能使用映射到 XmlnsDefinition
定义的XAML命名空间的控制,如果控制同一程序中定义。不同的组件做工精细,虽然。你必须使用 CLR命名空间
定义这一点。
为什么要到你的控件添加到XAML默认命名空间?不这样做。这就像使用系统
命名空间的类,因为你不希望添加使用
指令为他们的命名空间。
I'm a bit tired of having to declare an xmlns in every xaml file and having to use prefixes for my custom controls. Is it possible to map a clr namespace to "http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
I tried the following in my AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation","MyOwnNamespace")]
but this doesn't seem to work. I still get a compile error like:
The tag 'MyCustomControl' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
Note: My controls are within the same assembly (I have a single assembly).
解决方案Unfortunately, you cannot use controls mapped to a Xaml namespace defined by XmlnsDefinition
, if the controls are defined in the same assembly. Different assemblies work fine though. You'll have to use the clr-namespace
definition for this.
Why would you want to add your controls to the Xaml default namespace? Don't do this. It's like using the System
namespace for your classes because you don't want to add using
directives for their namespaces.