基本上,我记得曾经有一段时间您可以使用以下内容:
(它似乎不再起作用 - 也许已弃用?)
我知道我可以像这样映射我的 XML 命名空间...
xmlns:xyzcon="clr-namespace:XYZ.Wpf.Controls"
xmlns:xyzcom="clr-namespace:XYZ.Wpf.Commands"
但是,我也知道我可以像这样映射我的 XML 命名空间(更干净),
[assembly: XmlnsPrefix("http://schemas.mycompany.com/netfx/xaml/presentation", "xyz")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Controls")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Commands")]
这使得它非常易于使用,因为我可以拥有所有的前缀,而不是 l、lv 等。我的 CLR 命名空间指向相同的 XML命名空间...
xmlns:xyz="http://schemas.mycompany.com/netfx/xaml/presentation"
该行使我可以访问 XYZ.Wpf.Controls 和 XYZ.Wpf.Commands。问题是,这仅在程序集已编译的情况下才有效。在同一程序集中工作时,我无法使用此技巧。
基本上,在同一个应用程序或程序集中,我希望在全局范围内将不同的 CLR 命名空间映射到相同的 XML 命名空间(这样在我的应用程序中我只需包含该 XML 命名空间)。有办法做到吗?
Basically, I remember that there was a time when you could use the following: <?Mapping ... >
(It doesn't seem to work anymore -- Deprecated perhaps?)
I know I can map my XML namespaces like so...
xmlns:xyzcon="clr-namespace:XYZ.Wpf.Controls"
xmlns:xyzcom="clr-namespace:XYZ.Wpf.Commands"
But, I also know that I can map my XML namespaces like so (much cleaner)
[assembly: XmlnsPrefix("http://schemas.mycompany.com/netfx/xaml/presentation", "xyz")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Controls")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Commands")]
which makes it very easy to use because instead having the l, lv, etc. prefix I can have all my CLR-Namespaces point to the same XML namespace...
xmlns:xyz="http://schemas.mycompany.com/netfx/xaml/presentation"
That line gives me access to XYZ.Wpf.Controls and XYZ.Wpf.Commands. The problem is that this only works if the assembly is already compiled. I can't use this trick when working within the same assembly.
Basically, from within the same app or assembly, I want to map different CLR namespaces to the SAME XML namespace on a global scale (that way throughout my app I simply have to include that one XML namespace). Any way to do that?
发布评论
评论(1)
看来仍然无法在 WPF 项目中映射本地命名空间。该书编程 WPF :使用 Windows Presentation Foundation 构建 Windows UI(2007 年发布)指出:
Visual Studio 中的 WPF 项目不能使用位于同一程序集中的 XAML 中由
XmlnsDefinitionAttribute
引入的命名空间。如果要引用本地定义的类型,则必须使用替代机制...“替代机制”是每个命名空间的可怕的
xmlns:local="..."
。我的理解是,执行程序集(在本例中为本地 WPF 程序集)会在所有引用程序集中搜索
XmlnsPrefixAttribute
和XmlnsDefinitionAttribute
,但不搜索本身。四年后这种行为没有改变是没有道理的,但事实似乎就是如此。It appears that there is still no way to map local namespaces within a WPF project. The book Programming WPF: Building Windows UI with Windows Presentation Foundation (published in 2007) states:
WPF projects in Visual Studio cannot use namespaces introduced by
XmlnsDefinitionAttribute
from XAML that live in the same assembly. If you want to refer to locally defined types, you must use the alternative mechanism...The "alternative mechanism" being the dreaded
xmlns:local="..."
for each namespace.My understanding is that the executing assembly (in this case the local WPF assembly) searches all referenced assemblies for both
XmlnsPrefixAttribute
andXmlnsDefinitionAttribute
but does not search itself. It makes no sense that 4 years later this behavior has not changed, but that appears to be the case.