XmlnsDefinitionAttribute Is Pretty Nifty
2013-10-13 11:49
330 查看
When we want to declaratively use our custom controls or reference the types we defined in XAML, we should declare the XML namespace to map to the CLR namespace in which the controls and types are defined as the following XAML snippt demonstrates:
<Page x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="clr-namespace:Sheva.Windows.Controls;assembly=AvalonLib"/>
Recently, I am heavily engaged with writing a resuable WPF control library, and quite often I need to create some pages to test those controls I create, so ever now and then I have to use the cumbersome syntax like above to declare the XML namespace, personally I don't like the syntax, and I thought Microsoft WPF team should come up with a more elegant way of doing it, and in actuality, they did introduce an alternative to do it since the CTP releases. there is a custom attribute called XmlnsDefinitionAttribute in the System.Windows.Markup namespace which allows you to apply to the assembly which contains the types and controls the XAML code would reference.the built-in WPF assemblies such as WindowsBase.dll, PresentationCore.dll, and PresentationFramework.dll all have this attribute applied, and all have http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace mapped with the CLR namespaces introduced by them. and of cause you can use this attribute in your own project, just open the AssemblyInfo.cs file, and append the following directive:
[assembly: XmlnsDefinition("http://schemas.sheva.com/wpf/", "Sheva.Windows.Controls")]
Then you can declare the namespace alias in XAML this way:
<Page x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="http://schemas.sheva.com/wpf/"/>
Wow, this is a bit like the built-in xmlns declaration indeed, really really cool indeed:)
<Page x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="clr-namespace:Sheva.Windows.Controls;assembly=AvalonLib"/>
Recently, I am heavily engaged with writing a resuable WPF control library, and quite often I need to create some pages to test those controls I create, so ever now and then I have to use the cumbersome syntax like above to declare the XML namespace, personally I don't like the syntax, and I thought Microsoft WPF team should come up with a more elegant way of doing it, and in actuality, they did introduce an alternative to do it since the CTP releases. there is a custom attribute called XmlnsDefinitionAttribute in the System.Windows.Markup namespace which allows you to apply to the assembly which contains the types and controls the XAML code would reference.the built-in WPF assemblies such as WindowsBase.dll, PresentationCore.dll, and PresentationFramework.dll all have this attribute applied, and all have http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace mapped with the CLR namespaces introduced by them. and of cause you can use this attribute in your own project, just open the AssemblyInfo.cs file, and append the following directive:
[assembly: XmlnsDefinition("http://schemas.sheva.com/wpf/", "Sheva.Windows.Controls")]
Then you can declare the namespace alias in XAML this way:
<Page x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cc="http://schemas.sheva.com/wpf/"/>
Wow, this is a bit like the built-in xmlns declaration indeed, really really cool indeed:)
相关文章推荐
- 极化SAR图像基础知识(2)
- Tempter of the Bone
- java的学习法测!
- HD Tune Pro: WDC WD1600AAJS-00B8D 160g inMyRitMachi
- 连接各种数据库语句
- Gnome Tetravex
- 详解udev
- 解决webview加载网页出现白屏的问题
- pygame系列_font游戏字体_源码下载
- UESTC 1307 windy数 第一次的数位dp
- 井盖为什么是圆的?
- 第七周上机实践项目——求并联阻值
- linux系统下文件和目录的权限
- 使用SQL语句直接保存图片
- Html笔记(五)表格
- Team Queue UVA 540
- Tomcat 自启动配置 配置 Tomcat 服务
- 建立在不同价值观上的讨论没有任何意义的
- Class Extensions -匿名类别
- S3C2440-时钟