【Silverlight】Custom Namespaces

Custom Namespaces
In many situations, you’ll want to have access to your own namespaces in a XAML file. The
most common example is if you want to use a custom Silverlight control that you (or another
developer) have created. In this case, you need to define a new XML namespace prefix and map
it to your assembly. Here’s the syntax you need:

1 <UserControl x:Class="SilverlightApplication1.MainPage"
2 xmlns:w="clr-namespace:Widgets;assembly=WidgetLibrary"
3 ... >

   

The XML namespace declaration sets three pieces of information:
• The XML namespace prefix: You’ll use the namespace prefix to refer to the namespace in
your XAML page. In this example, that’s w, although you can choose anything you want
that doesn’t conflict with another namespace prefix.
• The .NET namespace: In this case, the classes are located in the Widgets namespace. If
you have classes that you want to use in multiple namespaces, you can map them to
different XML namespaces or to the same XML namespace (as long as there aren’t any
conflicting class names).
• The assembly: In this case, the classes are part of the WidgetLibrary.dll assembly. (You
don’t include the .dll extension when naming the assembly.) Silverlight will look for that
assembly in the same XAP package where your project assembly is placed.
posted @ 2010-01-15 13:37  道场  阅读(246)  评论(0)    收藏  举报