惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
罗磊的独立博客
U
Unit 42
I
InfoQ
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
S
SegmentFault 最新的问题
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园_首页
腾讯CDC
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
D
Docker
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
V
V2EX
Last Week in AI
Last Week in AI
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
L
LangChain Blog
WordPress大学
WordPress大学
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) WPF非轮询方式实时更新数据库变化SqlDependency 动态解析XAML文本构建WPF的UI 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(五)XML Query-XQuery SQL Server操作XML(四)XML数据类型 SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 SQL Server操作XML(一)XML子句 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之二数据绑定 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
照猫画虎WPF之一:命名空间
挑战 · 2012-03-09 · via 博客园 - 挑战

      开发语言会将常用功能以类的形式封装,开发人员根据自己的业务需求,也会封装满足自身业务需求的类,如果有序组织这些类?一方面,便于开发人员准确调用;另一方面,编译器可以有效识别具有相同命名的类,就引入了命名空间,简单的说,是通过类似树状结构来组织各种类,是一种较为有效的类名排列方式。

      而XAML和.NET其他语言一样,也是通过命名空间有效组织起XAML内部的相关元素类,这里的命名空间与.NET中的命名空间不是一一对应的,而是一对多,都一眼望去,都是“网址”这里的网址,是遵循XAML解析器标准的命名规则,而不是真正的网址(在IE中根本打不开)。例如

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

对应的.NET的命名空间:
. System.Windows
. System.Windows.Automation
. System.Windows.Controls
. System.Windows.Controls.Primitives
. System.Windows.Data
. System.Windows.Documents
. System.Windows.Forms.Integration
. System.Windows.Ink
. System.Windows.Input
. System.Windows.Media
. System.Windows.Media.Animation
. System.Windows.Media.Effects
. System.Windows.Media.Imaging
. System.Windows.Media.Media3D
. System.Windows.Media.TextFormatting
. System.Windows.Navigation
. System.Windows.Shapes

包含了XAML基本的布局和控件。
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"对应一些与XAML语法和编译相关的CLR名称空间。

 例如, <Style x:Key="buttonMouseOver" TargetType="{x:Type Button}">

这里的xmlns和xmlns:x,区别在于x作为http://schemas.microsoft.com/winfx/2006/xaml别名,在应用时,以前缀形式出现,而xmlns作为默认命名空间,不使用前缀标识的元素,来自该命名空间。

     如果添加.NET的命名空间,其引用格式如下:xmlns="clr-namespace:System.Data.Odbc;assembly=System.Data"

clr-namespace:System.Data.Odbc 导入的命名空间

assembly=System.Data 命名空间所在程序集

     举个例子说明,开发一个自定义的TextBox控件,使带有默认文本和背景颜色。

自定义控件为:

namespace MySpace.MyControl
{
    class MyControl:TextBlock
    {
        public MyControl()
        {
            this.Text = "自定义TextBlock!";
            this.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));                 
        }

    }
}

XAML中添加引用,并实例化该自定义控件

<Page x:Class="MySpace.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d" 
      xmlns:local="clr-namespace:MySpace.MyControl"
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page1" >
    <Grid>
        <local:MyControl Width="100" Height="50">
           
        </local:MyControl>
    </Grid>
</Page>

显示结果为

     这里,导入自定义控件的命名空间时,没有指定"assembly=****”,是因为采自定义控件是以类的形式存在于项目中,而非DLL,如果封装成DLL时,需要添加引用并指定该内容。