[XAML]命名空间xmlns

在编写WPF的XAML文件时,我们都会看到系统生成的默认文件里,包含如下东西:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 那么他们是什么意思呢?

xmlns是XAML引用的命名空间

不带冒号的,是作为默认命名空间,例如:

<TextBlock />

 带冒号的,在XML里就需要用前缀来引用,例如:

<x:Code />

 当你复制上面引用的地址到浏览器里打开时,是访问不到的。

它们并不是真正存在于网络上,而是存在于程序集里。

用Reflector反射PresentationFramework程序集

注意看程序集的信息,里面有一堆这样的代码:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Documents")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shapes")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shell")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Navigation")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Data")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls.Primitives")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Media.Animation")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Input")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Media")]

这些代码的作用就是把那些命名空间合并到一个统一的命名空间里,方便调用。

我们在自己的程序集里也可以这样用,只要修改AssemblyInfo.cs就可以了。

然后我们在XAML里添加命名空间,就会看到自己定义的命名空间了。

posted @   Kation  阅读(1445)  评论(0)    收藏  举报
编辑推荐:
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
阅读排行:
· 博客园2025新款「AI繁忙」系列T恤上架
· Avalonia跨平台实战(二),Avalonia相比WPF的便利合集(一)
· C# LINQ 快速入门实战指南,建议收藏学习!
· Redis实现高并发场景下的计数器设计
· 上周热点回顾(4.7-4.13)
点击右上角即可分享
微信分享提示