I wanted to touch on this briefly as I ran into a namescoping issue reading on animations and thought I'd share a little bit of what I learned.
A namescope is the relationship between the name of an object defined in XAML and its instance. This is particularly relevant when interacting with the object in the code behind. Framework level items (FrameworkElement / FrameworkContentElement can be named using the Name property in code or XAML attribute. However, some some animatable objects such as those that derive from the Freezable class requires using the x:Name XAML attribute value in order for code behind interaction with that instance of the object. The name assigned to an object is unique and this is enforced by the namescope of the containing object.
The INameScope interface provides the contract for enforcing name uniqueness and how names are accessed in a XAML namescope. The NameScope class implements base support for the INameScope methods that store or retrieve name-object mappings into a particular namescope, and adds attached property support to make it simpler to get or set identifier namescope information dynamically at the element level (from documentation).
Some things have been added and shifted around from .NET 3.5 to 4.0 Beta 1 for NameScope.
- NameScope only implements INameScope interface
- NameScope is under the System.Windows namespace in the PresentationFramework.dll assembly.
.NET 4.0 Beta 1
- NameScope implements several interfaces and provides a host of additional members
- moved to the System.Windows namespace in the WindowsBase.dll assembly.
While namescopes can be nested, because of the enforcement of uniqueness, two elements that falls within the same namescope cannot have the same name. For example, when you fire up Visual Studio and create a new WPF project (page or window), a namescope is automatically created for you since these are root elements of an application. Even if you do not assign names to any of the elements in XAML, the namescope is still there and waiting for you. But if you try to give two buttons on the same window the same name, an exception will be thrown. However, things may get a little weird when you jump to C# and try to work with objects from the code behind, especially when dealing with animation. Thus far, you can pretty much do a whole lot in XAML to satisfy your animation needs without sweating over the whole namescope issue.
This is where I ran into my first namescoping problems - writing animation code in C#. Here, I will look at simple snippets of code in two scenarios:
 A empty WPF project was created with Visual Studio
 A WPF app is written without any initial XAML at all - like in Notepad
As soon as you create a new WPF project in VS, remember that some initial housekeeping is automatically taken care for you, such as creating a root element, be is a Page or a Window. At this point, since this is root element, a namescope is created for it. Now if you add elements using VS either using XAML or the designer, then add the Name or x:Name attribute, all is well in the world and you can freely use the named elements in code behind with no issues.
But here, let's forget the XAML part and do it all in C# in the code behind. Therefore, when you created your WPF project (let's say window), all you have is the basic structure of a window and a grid as its content. Remove the grid and go to the code behind.
The code will create a rectangle object, name it, register its name in the containing namescope, create an animation to change the opacity, create a storyboard to run the animation.
Lines 32, 39 and 40 is where the namescoping comes in. We needed to set the name of the rectangle and register it in the namescope of the window (since its the root element, but you could have registered it to another containing elements' namescope). This is necessary for Line 59, setting the Storyboard.SetTargetName attached property. When the storyboard starts, it will look of the element with that particular name. It will first search any locally defined namescopes to find the name-object mapping then walk up the tree all the way to the root. If you did not register the name of the rectangle (you can comment out Lines 39,40 to see) then when you run the storyboards, guess what will happen. Nothing! That was because the storyboard could not resolve the name of the element it is supposed to use to animate a property on.
This is an instance if you do not use any XAML at all, probably not even using VS to create an initial skeleton project. Let's say you want to do exactly the stuff in case 1, but all in notepad. The main changes will be as shown:
The main points are Lines 3 and 19. Because we did not have an initial XAML file, no namescope was automatically created for us to use. However, in Line 3, we created our own and in Line 19 we registered the rectangle to be animated with the namescope. Make note of the slight syntax differences.