본문 바로가기
C#(CSharp)/WPF

XAML Custom Namespace

by swconsulting 2015. 4. 8.

A Guide to Cleaner XAML with Custom Namespaces and Prefixes (WPF/Silverlight)


When working with any type of application, it's a good idea to split up your solution in multiple projects (assemblies) and namespaces. As your project grows, you'll see your amount of namespaces grow with it. And if you plan to use classes / controls from these namespaces in your XAML code, you'll have to declare them with the following syntax:

xmlns:PREFIX="clr-namespace:NAMESPACE" 

An example could be:

xmlns:conv="clr-namespace:Sandworks.Silverlight.NamespaceExample.Converters" 

If you are referencing an other assembly, you'll also need to add the assembly name:

xmlns:PREFIX="clr-namespace:NAMESPACE;assembly=ASSEMBLYNAME" 


-clr-namespace: 요소로 노출되는 공용 형식을 포함하는 어셈블리 내에서 선언된 CLR 네임스페이스입니다.

-assembly= 참조된 CLR 네임스페이스 일부 또는 전체를 포함하는 어셈블리입니다. 이 값은 일반적으로 경로가 아니라 어                     셈블리의 이름이며 .dll 또는 .exe와 같은 확장명을 포함하지 않습니다. 해당 어셈블리의 경로는 매핑하려는                       XAML을 포함하는 프로젝트 파일에서 프로젝트 참조로 설정해야 합니다. 버전 관리와 강력한 이름 서명을 통                     합하기 위해 assembly 값은 간단한 문자열 이름 대신 AssemblyName으로 정의된 문자열일 수 있습니다.

-clr-namespace 토큰과 해당 값을 구분하는 문자는 콜론(:)이지만, assembly 토큰과 해당 값을 구분하는 문자는 등호(=)입                     니다. 이 두 토큰 간을 구분하는 데 사용되는 문자는 세미콜론입니다. 또한 선언에는 공백을 포함하면 안 됩                       니다.


For example:


xmlns:lib="clr-namespace:Sandworks.Silverlight.NamespaceExample.ClassLibrary.Converters
     ;assembly=Sandworks.Silverlight.NamespaceExample.ClassLibrary"

Some namespaces are added by default when you add a new XAML control in your project (be it a UserControlor a Page).
This is how it should look like:

<UserControl x:Class="Sandworks.Silverlight.NamespaceExample.MainPage" 
   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"

But, as stated before, when your project starts growing and you need to work with different namespaces and assemblies, your references might start piling up and your XAML code gets bloated.


출처 : http://www.codeproject.com/Articles/111911/A-Guide-to-Cleaner-XAML-with-Custom-Namespaces-and