我有点厌倦了必须在每个xaml文件中声明一个xmlns并且必须为我的自定义控件使用前缀.是否可以将clr命名空间映射到"http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
我在AssemblyInfo.cs中尝试了以下内容:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation","MyOwnNamespace")]
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.我仍然得到一个编译错误,如:
XML名称空间"http://schemas.microsoft.com/winfx/2006/xaml/presentation"中不存在标记"MyCustomControl".
注意:我的控件在同一个程序集中(我有一个程序集).
遗憾的是,XmlnsDefinition如果控件是在同一个程序集中定义的,则不能使用映射到由其定义的Xaml命名空间的控件.不同的组件工作正常.你必须使用这个clr-namespace定义.
为什么要将控件添加到Xaml默认命名空间?不要这样做.这就像System为类使用命名空间一样,因为您不希望using为其命名空间添加指令.