Namespace in xaml
来源:互联网 发布:血族 知乎 编辑:程序博客网 时间:2024/05/21 17:02
传统的我们在xaml中引用自己的类型都需要写xmlns,比如说,
在WpfApplication1中定义Test01
namespace WpfApplication1 {
public class Test01 : UIElement { }
}
在ClassLibrary1中定义Test02
namespace ClassLibrary1 {
public class Test02 : UIElement { }
}
然后在WpfApplication1中的MainWindow中引用这两个UIElement就需要导入namespace:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ns1="clr-namespace:WpfApplication1"
xmlns:ns2="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ns1:Test01 />
<ns2:Test02 />
</StackPanel>
</Window>
总之一个namespace就要对应一个xmlns,有时候动不动会写上不下10个,惨不忍睹,开始以为xmlns:x是wpf的特权,后来看到Charles Petzold的Media3D也采用了和xmlns:x一致的手法,我就知道有戏,
xmlns:cp="clr-namespace:Petzold.Media3D;assembly=Petzold.Media3D"
MSDN了一下,查到需要用到XmlnsDefinitionAttribute,通过此属性可以将uri和多个namespace关联,修改上述代码,在AssemblyInfo中加入
[assembly: XmlnsDefinition("http://nonocast.cn/wpf/xaml", "ClassLibrary1")]
[assembly: XmlnsDefinition("http://nonocast.cn/wpf/xaml", "WpfApplication1")]
然后引用namespace
xmlns:my="http://nonocast.cn/wpf/xaml"
...
<my:Test01 />
<my:Test02 />
Well,无法编译通过,说找不到Test01,如果只保留Test02是OK的,经查证,xaml通过扫描其关联的assemly反射出XmlnsDefinition的Atribute进而提示,而不会加载自身assembly,有点奇怪,不过这就是事实,在MSDN中的Remark也提到了。
- Namespace in xaml
- XAML 自定义 namespace
- XAML实例教程系列 - 命名空间(NameSpace)
- XAML实例教程系列 - 命名空间(NameSpace)
- WPF学习笔记1:XAML之NameSpace
- namespace in javascript
- xml namespace in android
- [CHM]果壳中的XAML(XAML in a Nutshell)
- [CHM]果壳中的XAML(XAML in a Nutshell)
- forward declare class in namespace
- namespace in c++用法详解
- XAML入门-Silverlight 1.1 In Action
- WPF: Animations in XAML and code-behind
- WPF: Animations in XAML and code-behind
- Silverlight 4 Binding and StringFormat in XAML
- XAML
- XAML
- XAML
- 为dropdownlist循环绑定1-100
- 利用C#实现分布式数据库查询
- 参数化SQL中 Like 和 In 的写法
- Hadoop 解除 "Name node is in safe mode"
- Hibernate中get和load方法的区别
- Namespace in xaml
- Python定时执行之Timer
- Thinkphp 自定义错误页面
- Adapter性能提升
- S3C6410(arm11核)的DM9000移植
- 设计模式有哪些?工厂模式、适配器模式、策略模式等等 框架模式有哪些?MVC、MTV、MVP、CBD、ORM等等;
- VC++实现FTP编程 ?
- Mahout关联规则算法源码分析--如何分数据
- free命令