【问题标题】:Why is XAML(WPF) not searching in my XML namespace definitions for the controls?为什么 XAML(WPF) 不在我的 XML 命名空间定义中搜索控件?
【发布时间】:2015-08-19 06:18:41
【问题描述】:

我有两个类似的项目。一个是 Silverlight 项目,另一个是 WPF。它们都包含一些命名空间和大量自定义用户控件。

由于控件分布在许多命名空间中,因此在使用它们时,我必须定义相当多的命名空间。于是我开始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

【问题讨论】:

  • 您对 WPF 和 Silverlight 使用相同的程序集吗?也许 WPF 不能使用 Silverlight 控件
  • 不,我已经将此作为一个最小示例进行了测试。两个项目,一个silverlight和一个wpf。无论如何,它们都没有链接。两者都有自定义用户控件和显示方式。
  • 我说的是帖子中的实际项目,因为我想给这个问题一个背景/背景。

标签: c# xml wpf xaml silverlight


【解决方案1】:

尽管本教程适用于 Silverlight,但我相信它可能对 WPF 不准确。查看接受的答案here

我在 WPF 中有一个带有自定义控件的单独程序集,我可以毫无问题地使用 XmlnsDefinition 属性。我认为问题在于在构建程序集之前需要存在的定义。因此,即使控件将显示在您的设计器中,也无法构建它,因为编译器在编译 BAML 中的 XAML 时需要定义 built into the assembly at a later point

编辑

我创建了另一个 WPF 用户控件库并添加了您使用的相同 XmlnsDefinitions,将用户控件库的引用添加到我的 WPF 应用程序项目中,在我的 MainWindow.xaml 中使用了 <cui:UserControl1 /> 并且没有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多