您的位置:首页 > 其它

XAML共享命名空间及前缀

2012-07-25 01:15 197 查看
  由于Silverlight SDK或是第三方控件都未能满足项目需求,因此近一年来一直在专注Silverlight控件开发,本可以全用UserControl完成,但考虑控件的复用时候需要修改样式,因此不得不转向Silverlight模板控件。

  在xaml中引用命名空间时候常会看到

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

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

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

  使用sdk,toolkit前缀就可以访问很多Silverlight元素,尽管它们不在同一个程序集或命名空间下。

  一般情况,如果自己写个程序集(不管是Silverlight元素,还是MVVM中VM),在xaml中引入时候都会这样:

    xmlns:vm="clr-namespace:..., assembly=..."

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

  如此一来,自定义程序集中一般都会有多个命名空间,那么如何可以像sdk,toolkit这样,将所有命名空间都映射到同一个前缀下面?个人感觉这样用起来方便一些。

  虽然自己理解这个问题,但不知道该使用什么术语来表达这个问题,网上也不知道该如何搜索问题....

  终于通过查看sdk和toolkit中的程序集(使用Reflector查看system.windows.controls.dll)找到解决方法:

  修改assemblyinfo.cs, 该文件在工程项目的Properties下,修改后类似一下代码

View Code

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Markup;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Metislight.Toolkit.Extension")]
[assembly: AssemblyDescription("扩展Silverlight toolkit应用")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("METISRIA")]
[assembly: AssemblyProduct("Metislight.Toolkit.Extension")]
[assembly: AssemblyCopyright("Copyright © Metisria 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit", "Metisria.Metislight.Toolkit.Controls")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit", "Metisria.Metislight.Toolkit.DataVisualization")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit", "Metisria.Metislight.Toolkit.DataVisualization.Series")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit", "Metisria.Metislight.Toolkit.Dialogs")]
[assembly: XmlnsPrefix("http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit", "toolkit")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9b30f946-41a8-4119-9547-0ecc7e7a3ccd")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.1.106.0")]
[assembly: AssemblyFileVersion("6.1.106.0")]


  由于是扩展Toolkit,因此想把自己定义的程序集也映射到toolkit前缀下。

  XmlnsDefinition:定义命名空间映射关系; XmlnsPrefix:定义命名空间前缀

  如果程序集中不存在某命名空间,但却使用 XmlnsDefinition 将其定义到指定的xmlns中,可以编译成功,但在其它工程引用该程序集时候会发生编译错误。

  现在越来越喜欢做控件开发了...这一过程可以大量学习Silverlight设计理念和特性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: