发布于 2018-05-22 13:21 更新于 2018-09-01 00:04
在 WPF XAML 中使用自己定义的控件时,想必大家都能在 XAML 中编写出这个控件的命名空间了。然而——我写不出来,除非借助 ReSharper。
如果控件能够有一个漂亮的命名空间和命名空间前缀呢?——好吧,还是写不出来,不过,至少漂亮些。本文将指导你自定义在 XAML 中使用的命名空间。
<UserControl
x:Class="HuyaHearhira.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:w="http://walterlv.github.io/demo"
<Grid>
<w:DemoPage />
</Grid>
</UserControl>
注意到 DemoPage
所在的命名空间了吗?是 http://walterlv.github.io/demo
哦。而且,命名空间前缀是 w
。这是不是比下面这种看得清爽多了呢?
<UserControl
x:Class="HuyaHearhira.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:newCsprojDemo="clr-namespace:Walterlv.NewCsprojDemo;assembly=Walterlv.NewCsprojDemo">
<Grid>
<newCsprojDemo:DemoPage />
</Grid>
</UserControl>
当然,好处不止是更清爽,还有更多,总结起来是这三个:
clr-namespace
和 assembly
。在 System.Windows.Markup
命名空间下,有两个程序集级别的 Attribute
,分别是 XmlnsDefinition
和 XmlnsPrefix
。XmlnsDefinition
定义某个 C# 命名空间和一段命名空间字符串是等意的,XmlnsPrefix
定义此命名空间的默认前缀(只是默认而已)。
using System.Windows.Markup;
[assembly: XmlnsDefinition("http://walterlv.github.io/demo", "Walterlv.NewCsprojDemo")]
[assembly: XmlnsPrefix("http://walterlv.github.io/demo", "w")]
于是,利用这两个 Attribute
能够达到本文一开始的奇妙的效果。
如果你用工具(例如 ReSharper)自动生成命名空间前缀时,才会使用这样默认的命名空间前缀,否则,你随便填。
也许你注意到 WPF 有一些一开始就帮你生成好的命名空间前缀,例如这些:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
</UserControl>
如果我们也把命名空间定义到这里会如何呢?
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Walterlv.NewCsprojDemo")]
哇,我们竟然可以不用带前缀啦!
<UserControl
x:Class="HuyaHearhira.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<DemoPage />
</Grid>
</UserControl>
这在项目内为一些几乎侵染全部代码的标记扩展是很棒的一波语法糖。例如——自己实现的本地化标记扩展。
值得注意的是,XAML 命名空间的定义只会在外部程序集生效。这是说,如果你在 A 程序集中定义了命名空间,那么只有引用了 A 程序集的 B 或者 C 才可以使用到新定义的命名空间;A 程序集自身是没有办法使用此命名空间的。
本文会经常更新,请阅读原文: https://walterlv.com/post/define-xmlns-of-for-xaml.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (walter.lv@qq.com) 。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有