我有两个类似的项目。一个是 Silverlight 项目,另一个是 WPF。它们都包含一些命名空间和大量自定义用户控件。
由于控件分布在许多 namespace 中,因此在使用它们时我必须定义相当多的 namespace 。所以我开始define the XML namespaces in the AssemblyInfo.cs
:
[assembly: XmlnsPrefix("http://ui.example.com/xaml/touch", "cui")]
[assembly: XmlnsDefinition("http://ui.example.com/xaml/touch", "example_ui.controls")]
[assembly: XmlnsDefinition("http://ui.example.com/xaml/touch", "example_ui.themes")]
现在我只需要在每个文件中定义一个命名空间:
xmlns:cui="http://ui.example.com/xaml/touch"
不幸的是,这仅适用于 Silverlight。我的问题是,如何让它在 WPF 项目中工作?
在 WPF 项目中我得到这样的错误:
Error 5 The tag 'LookUpField' does not exist in XML namespace
'http://ui.example.com/xaml/touch'. Line 7 Position 14. D:\src\prototype\lookup-control\lookup-control\MainWindow.xaml 7 14 lookup-control
最佳答案
虽然该教程适用于 Silverlight,但我认为它可能不适用于 WPF。查看已接受的答案 here .
我在 WPF 中有一个带有自定义控件的单独程序集,我可以使用 XmlnsDefinition
属性没有问题。我认为问题在于在构建程序集之前需要存在的定义。因此,即使控件会出现在您的设计器中,也无法构建它,因为编译器在 BAML 中编译 XAML 时需要定义,即 built into the assembly at a later point。 .
编辑
我创建了另一个 WPF 用户控件库并添加了相同的 XmlnsDefinition
如果你使用过,将用户控件库的引用添加到我的 WPF 应用程序项目中,使用过 <cui:UserControl1 />
在我的 MainWindow.xaml
并且没有错误。
关于c# - 为什么 XAML(WPF) 不在我的 XML 命名空间定义中搜索控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087833/