首页 文章

Clr名称空间映射到默认的xaml名称空间

提问于
浏览
7

我有点厌倦了必须在每个xaml文件中声明一个xmlns并且必须为我的自定义控件使用前缀 . 是否可以将clr命名空间映射到“http://schemas.microsoft.com/winfx/2006/xaml/presentation”?

我在AssemblyInfo.cs中尝试了以下内容:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation","MyOwnNamespace")]

但这似乎不起作用 . 我仍然得到一个编译错误,如:

XML名称空间“http://schemas.microsoft.com/winfx/2006/xaml/presentation”中不存在标记“MyCustomControl” .

注意:我的控件在同一个程序集中(我有一个程序集) .

1 回答

  • 9
    • 遗憾的是,如果控件是在同一个程序集中定义的,则不能使用映射到 XmlnsDefinition 定义的Xaml命名空间的控件 . 不同的组件工作正常 . 你必须使用 clr-namespace 定义 .

    • 为什么要将控件添加到Xaml默认命名空间?因为您不希望为其命名空间添加 using 指令,所以就像为您的类使用 System 命名空间一样 .

相关问题