views:

211

answers:

1

In Silverlight/XAML you have namespaces such as:

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

and so elements have namespaced attributes like this:

<TextBlock x:Name="theMessage" Margin="10">Testing...</TextBlock>

When would this be a benefit for me? Would I at some point create another namespace, e.g.:

xmlns:edward="http://www.tanguay.info/web"

so I can put my own name attributes tags, e.g.:

<TextBlock x:Name="theMessage" edward:Name="secondName" Margin="10">Testing...</TextBlock>

And then somehow process both of the name tags, etc.?

+1  A: 

XAML is a XML based markup language, thus you can take advantage of namespaces. The primary goal for this approach is to organize your work in smaller units and mantain disambiguity between them. It's the same principle that operates with normal namespaces in .NET (or other programming languages). Tipically in XAML you use

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

namespace to address the base controls (remember that it's just a string, not an address pointing actually to a website). Other than that, it's common to find reference to other namespaces used to connect to other assemblies (third party or you own) containing business logic or other XAML objects.

xmlns:demo="clr-namespace:MyNamespace;assembly=MyNamespace.Lib"

and in your XAML have something like this

<Grid>
      <demo:MyCustomControl />
</Grid>

Where MyCustomControl is control defined in MyNamespace.Lib assembly.

EDIT: just remembered, if you want to keep a XAML-like syntax in your namespace references, you can create alias for them in the form of uri. Check out this example.

Stefano Driussi