Thursday, February 18, 2010

Do the URL's at the top of a XAML file try to access the internet

At the top of a xaml file (after the Window declaration are the two lines below:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

The question came up if these two files cause every WPF application to try to access the Internet.  The answer is a resounding "NO".  What those two lines do is they declare a namespace.  In XML language one uses the term "xmlns" to declare namespaces.  The first line declares a namespace called "http://schemas.microsoft.com/winfx/2006/xaml/presentation" and the second line declares a namespace called "http://schemas.microsoft.com/winfx/2006/xaml".  Its not a pointer to a website, its just a name for a namespace, just like "Ted" is my name.  If I changed my name to "www.ted.com" then that would be my name, not necessarily a pointer to a website.

The difference between the first and second lines is the ":x" that is on the second line.  This basically maps the namespace associated with the second line has a prefix associated with it called "x".  "That means you can use controls in that namespace just by placing the namespace prefix before the element name (as in <x:ElementName>).  Its basically a shortcut to get to a namespace.

The first line doesn't have a prefix, so its the default namespace for the entire file.  This means that if you use a grid control and use the following <Grid>, then the XAML parser will look for that Grid control in the .NET namespaces until it finds System.Windows.Controls.Grid.

The reason its added to the top of every file, is to make sure other companies won't create different XML-based languages with the same namespace. 

Another example is that recently we purchased DevExpress and got their DXGrid control.  The following namespace was declared on some sample code:
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"

Now my understanding is that they have a namespace called "http://schemas.devexpress.com/winfx/2008/xaml/grid" and assigning a prefix called "dxg" to it.  So if I wanted to use their grid, I would do the following <dxg:GridControl ...>.  When I installed their software they installed their GridControl in the namespace "http://schemas.devexpress.com/winfx/2008/xaml/grid", so when the XAML parser runs, it would look on my computer for the namespace "http://schemas.devexpress.com/winfx/2008/xaml/grid" and try to find GridControl.

I hope that makes things clearer.



No comments: