Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There are hundreds if not thousands of types available for use in your Silverlight application. If you want to use a .NET type and a XAML file you need understand XAML namespaces. Using a XAML element in your Silverlight application is the same as instantiating a .NET class. To reduce the amount of text contained in the XAML file, Microsoft created the notion of namespace mapping. In .NET code it is common to put your types in segregated namespaces. For example code that works with files is stored in a .NET namespace called System.IO.
Code that works with databases is stored in a different namespace, System.Data. This prevents name collisions, especially when you're working with libraries of other developer's code. Let's look at the example on the slide. On line 23 in the example I'm trying to instantiate an instance of a class called FileInfo, so I declare a variable var fi1 =new System.IO.FileInfo, the namespace is System.IO. You can see on line 24 I have another class with the same name, but it is inside the Lynda namespace, so there is no conflict.
The compiler can find the type necessary. The XML world uses namespaces, too. Usually an XML namespace refers to a schema, which exists to help validate your XML tags. You can bind to these XML namespaces with the xmlns attribute. Let's look at the XML example on this slide. On line 2 I'm creating a trucks element and inside that trucks element are two namespace definitions, the first one xmlns="http://BigTruckCo" is considered the default namespace.
So when I go to line 6 or line 7 and work with the color element, that is being validated by the default namespace. On line 3 I've defined XML namespace with an alias, garage, and that points to a different unique URI. So on line 9 when I say garage:mechanic, that element is being validated inside the RepairCo namespace. Silverlight combines these two ideas. It has a mapping system that associates the XML's namespace with one or more .NET namespaces.
Silverlight puts the most common elements in a super namespace, the default namespace in Silverlight XAML. It is packed with hundreds of useful elements. This mapping identifies how the XAML parser knows that System.Windows.Controls.Button and how it knows that rectangle should be an instance of System.Windows.Shapes.Rectangle. This idea is called Namespace Mapping, when you take a .NET namespace and a Silverlight namespace and you map the two together. There are two kinds of namespace mapping.
I will call the first one Pre-assigned mapping. The idea here is that a developer creates an assembly containing some classes, somebody say at Microsoft. Let's say they create some classes and add them to a .NET namespace, for instance System.Windows.Shapes.Rectangle. This library developer then picks a unique identifier called a URI. They map their namespaces to the UI with the xmlns definition attribute. A consuming developer references their DLL and then uses the URI in their XAML file.
The scenario on the last slide is great, but in the real world not every library developer is going to mark their assembly with the correct attribute. So there has to be a way in XAML to reference code that doesn't have a predefined URI. I call this the Ad Hoc namespace. In this case the library developer creates an assembly containing some classes, but they don't predefine any mapping data. Now the consumer has to use a different syntax to reference the second library. It helps to see an example. In this example on line 3, 4 and 5 I am declaring three separate namespace declarations.
Line 3 is the default namespace declaration, and that maps to line number 7. So when I use the Grid element on line 7 or the Polygon element on line 9, those are coming from the default namespace. The Ad Hoc namespace is listed on line 5. Here I have set xmlns:lynda= and then this syntax specifies a couple bits. Over here on the right side I am specifying the assembly that contains the code and on the left-hand side I am specifying the .NET namespace that contains the types that I'm interested in.
So down on line 12 I can say, go to the Lynda namespace, get the .NET type called StarShape and place it here in this location. Now I would like to show you this in Visual Studio. I've opened a solution called Namespaces, this contains two projects and it has a Silverlight project and it has a Silverlight library called LyndaLib. Down here in this ShowNamespaceMapping.xaml file, I show the same items I showed you on the slide a few minutes ago.
I have an Ad Hoc namespace here and I have got the two default namespaces above. And as you see, when I come down here I have got Lynda:BlobbyControl. If I delete this and retype in the Colon (:) you'll see I get IntelliSense saying there is one class inside that namespace that I can use in my XAML file. I am going to go to my Toolbox. I am going to find a control called that DatePicker. This lives in a different namespace. Let me drag this to my designer surface and then a couple of things happen; for one it added reference to my project, and for number two it added a new namespace declaration.
This says xmlns:sdk and this is a predefined namespace that somebody in Microsoft created, a unique URI that they created. I'd like to improve my Lynda code here and change this, so BlobbyControl is available in a predefined namespace. To do that I go to my LyndaLib project, I open this file called AssemblyInfo.cs and then down here in my code I say I would like to link this URI, http://lynda.com/essential, this is not a real endpoint on the Internet, this is just a unique string.
This unique URI is tied to the LyndaLib namespace inside this project. So any classes that are defined inside this namespace are now available with this URI. I only have one Blobby Control. I am going to come back to my regular Silverlight application and I am going to show you how to change the mapping. I am going to go up to this line here, I am going to delete the text between the two single quotes. And then in Visual Studio I am going to do a Ctrl+Spacebar to show me the possible libraries.
It lists the top all of the predefined namespaces, so here you can see lynda.com/essential and then below that are all the Ad Hoc namespaces that you can add. So I'll go up here and choose this new predefined, and I'll come down here and delete this and reedit and you see that it hasn't changed. So now I am using the predefined namespace. A quick review of what we've seen. You can find hundreds of useful classes inside XAML namespaces and if you create your own classes they are accessible too.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98482 Viewers
61 Video lessons · 85764 Viewers
71 Video lessons · 69669 Viewers
56 Video lessons · 101993 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.