简体   繁体   English

CLR命名空间映射到默认XAML命名空间

[英]Clr namespace mapping to default xaml namespaces

I'm a bit tired of having to declare an xmlns in every xaml file and having to use prefixes for my custom controls. 我有点厌倦了必须在每个xaml文件中声明一个xmlns,并且必须对我的自定义控件使用前缀。 Is it possible to map a clr namespace to "http://schemas.microsoft.com/winfx/2006/xaml/presentation"? 是否可以将clr命名空间映射到“ http://schemas.microsoft.com/winfx/2006/xaml/presentation”?

I tried the following in my AssemblyInfo.cs: 我在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'. XML名称空间“ http://schemas.microsoft.com/winfx/2006/xaml/presentation”中不存在标签“ MyCustomControl”。

Note: My controls are within the same assembly (I have a single assembly). 注意:我的控件在同一程序集中(我有一个程序集)。

  1. Unfortunately, you cannot use controls mapped to a Xaml namespace defined by XmlnsDefinition , if the controls are defined in the same assembly. 不幸的是,如果控件在同一程序XmlnsDefinition定义,则不能使用映射到XmlnsDefinition定义的Xaml命名空间的控件。 Different assemblies work fine though. 不同的程序集可以正常工作。 You'll have to use the clr-namespace definition for this. 您必须为此使用clr-namespace定义。

  2. Why would you want to add your controls to the Xaml default namespace? 为什么要向Xaml默认名称空间添加控件? 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. 这就像为类使用System名称空间,因为您不想为它们的名称空间添加using指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM