WPF - 全局添加 xaml 命名空间声明 [英] WPF - globally add xaml namespace declaration

查看:47
本文介绍了WPF - 全局添加 xaml 命名空间声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的 WPF 应用程序,其中包含大量 XAML 文件.每个单个 XAML 文件都有 5 到 10 个 clr 到 xml 命名空间映射 xmlns:abc="clr-namespace:Abcdef".
看起来很糟糕,而且在每个文件中都写起来很痛苦.

I have quite a huge WPF application with a lot of XAML files. Every single XAML file has from 5 to 10 clr to xml namespace mappings xmlns:abc="clr-namespace:Abcdef".
It looks awful and is a pain to write in each and every file.

有没有办法全局定义这些?

Is there a way to define those globally?

推荐答案

无法跨文件全局定义它们.这是 XML 的限制;XAML 是它的一个子集.

There is no way to define them globally across files. This is a limitation of XML; XAML is a subset of it.

但是您可以使用 XmlnsDefinition

However you can clean them up a bit using XmlnsDefinition

参见这篇文章:http://zachbonham.blogspot.com/2010/04/organize-xaml-namespace-declarations.html

如果您开始使用此 XAML:

If you started with this XAML:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:treeView="clr-namespace:MaryKay.SamPortal.Common.UI.TreeView.Views;assembly=MaryKay.SamPortal.Common.UI"
    xmlns:infoBar="clr-namespace:MaryKay.SamPortal.Common.UI.InfoBar.Views;assembly=MaryKay.SamPortal.Common.UI">
  <infoBar:InformationBar DataContext="{Binding InfoBar}"/>
</UserControl>

并添加了这些 XmlnsDefinition 属性:

And added these XmlnsDefinition attributes:

[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.InfoBar.Views")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.RoleGroupPicker.Views")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.BetterPopup")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.TextEditor")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.Converters")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.Documents")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.SplashScreen")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.TemplateSelector")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.ModalDialog")]
[assembly: XmlnsDefinition("urn:marykay-samportal-common-ui", "MaryKay.SamPortal.Common.UI.ConsultantSearch.Views")]
// etc...

您最终可以使用此 XAML:

You could end up with this XAML instead:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:commonUI="urn:marykay-samportal-common-ui">
  <commonUI:InformationBar DataContext="{Binding InfoBar}"/>
</UserControl>

这篇关于WPF - 全局添加 xaml 命名空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆