Wpf之命名空间!


前言

Wpf之命名空间!

一、命名空间

我们的程序中有许多的命名空间,例如一个程序中有 Window 类–Window 类可能是指 System.Windows.Window类,也可能是指位于第三方组件中的 Window 类,或您自己在应用程序中定义的 Window 类等。为了弄清你实际使用的是哪个类,XAML解析器会检查应用于元素的XML名称空间。

二、命名空间讲解

<Window x:Class="WpfAppLearing.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfAppLearing"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

第一行代码x:Class="WpfAppLearing.MainWindow"不是命名空间他是对应的C#代码中的namespace WpfAppLearing命名空间下的MainWindow 类。
这些代码
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”
都是XAML的命名空间,看起来像个网址但是不是网址就是XAML的命名空间,每一行代码都代表着一种引用。
但是为什么的第一行是这种xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”
而下面的都是类似于这种的 xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”, 第一行的命名空间内的方法可以直接使用,而下面=号前面的字符就相当于给命名空间取了个别称(专业说法是命名空间映射到x上),例如我想要使用这个命名空间内的方法xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”,就需要

<Button x:Name="btn" />

xmlns:local="clr-namespace:WpfAppLearing"也是是个命名空间,这命名空间就是C#代码中的namespace WpfAppLearing

总结

xmlns 特性是 XML 中的一个特殊特性,它专门用来声明名称空间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值