Namescopes in Silverlight
What is a name scope?
Nick Kramer describes a name scope or namescope thusly:
"In almost all programming languages, names are not globally unique, they are unique only relative to other names in the same name scope. In C++ and C#, a name scope is roughly what goes between curly braces -- { }. And in C++ and C#, namescopes nest -- if the compiler can't find the name in the nearest namescope { }, it will look in the containing namescope.
Xaml names have many of the same issues. In <Button Name="foo">, foo is not unique to the whole program, there could be other xaml files with the same name. And you can instantiate that xaml file multiple times (e.g., multiple windows). "
Nick goes on to explain the role namescopes play in WPF. They kinda play a similar role in Silverlight.
Okay so we now know that a namescope is a logical grouping of named elements. Further, the "root" or ownership of a namescope is tied to a specific element and all of its named children fall into that namescope unless a child defines another namescope. This element is considered having the ownership of the namescope.
When do I care?
A namescope is typically created when the root visual is set. Well, in Silverlight there's a couple ways you, the app developer, can create elements in a separate namescope. One way is through a createFromXaml call in Javascript where you set the last parameter to true. The other is when you instantiate a Silverlight 1.1 Alpha control (see the SDK). In the latter case, the ControlBase class constructor makes a call to InitializeFromXaml in System.Windows.Controls.Control - which in a way is the managed equivalent of making a createFromXaml(xaml, createNamescope) call. You can use that approach for your custom controls.
Update: And oh, you also care when you do a FindName(). That method only walks the tree in the namescope of the element on which FindName was called. For instance, of you do rootCanvas.FindName("someElementName") you're searching for "someElementName" in the namescope of the rootCanvas element. Another element with the same name but different namescope is invisible at this time.
What types of namescopes exist?
The namescope created with the root visual is called the default namescope. When you call createFromXaml and tell it to create a namescope this is called a private namescope. If you call createFromXaml and do not create a namescope a temporary namescope is created
All elements content created in the visual tree will reside inside the namescope. Names defined in the namescope will not conflict with other names outside of the namescope.
Default namescope
A default namescope is automatically created when the root visual is created. The default namescope can be replaced by resetting the main visual tree.
Private namescope
A private namescope is the permanent name scoping that is created. A private namescope has the restrictions mentioned earlier in that it cannot be dissolved – it is always associated with the element that owns the private namescope. Named/un-named elements can be added/remove from the private namescope though.
Temporary Namescope
A temporary namescope is created any time you call createFromXaml and have an element tree fragment not associated with the main tree. A temporary namescope is different from a private namescope in that when you add the element tree fragment to another tree the temporary namescope is dissolved. This "addition" can be by using a collection's Add method or by setting a property such as a Rectangle's Fill.
Labels: Controls, name scope, Namescope, Parser, Silverlight
12 Comments:
My solution up until now was to create a "UniqueID" (based on an ASP.Net control hierachy IDs and seperator) by replacing the "Name=" string with "Name=RootElement_Parent_Son" structure before calling initializeFromXaml. (and it has to be done at the loaded event so I can get the parent ID and the current control ID).
Is there a better way? Can I use temporary namescopes? Is it better to keep everything In a private namescope?
(Please do ping me by email if you answer these questions - J@JustinAngel.Net)
By Anonymous, at August 17, 2007 at 2:19 PM
Hi,
Could you please share an example on how you used createfromXaml? I like to have 2 buttons and when I click on each diferent child xaml file + their dependant files (js/cs) be loaded
in the same frame of the main canvas.
Thanks a bunch
Mani
http://mani.golparvar.googlepages.com
By Mani, at August 17, 2007 at 7:15 PM
For mani: try to search video tutorials on this. As I remember there is such a video on http://codervods.com covering this subject. I hope it will help you
By CodingFather, at July 2, 2008 at 11:46 PM
This is a great site. Thank you for your information. I THANK YOU I SALUTE YOU IT,S A AMZING SITE.
By Anonymous, at July 14, 2008 at 2:18 PM
Few have wow gold even heard wow gold of the god buy wow gold called Bandos. buy wow gold For many cheap wow gold centuries he cheap wow gold has been thought wow power leveling to be an wow power leveling ancestor of the power leveling ogres, perhaps power leveling even the beast wow gold that claimed buy wow gold the lands of cheap wow gold the Feldip Hills world of warcraft gold and Jiggig.
By Anonymous, at February 16, 2009 at 7:26 PM
cheap wow gold
atlantica gold
atlantica online
cheap wow powerleveling
wow gold
wow power leveling
wow powerleveling
tibia gold
buy tibia gold
cheap tibia gold
By Anonymous, at April 2, 2009 at 7:27 PM
What do you know Pirates of the Burning Sea Gold. And do you want to know? You can get potbs gold here. And welcome to our website, here you can play games, and you will get potbs Doubloon to play game. I know potbs money, and it is very interesting.Do you want a try, come and view our website, and you will learn how to buy potbs Doubloon. Come and join with us. We are waiting for your coming.
What do you know Perfect World Gold. And do you want to know? You can Buy Perfect World Gold here. And welcome to our website, here you can play games, and you will get Perfect World Silver to play game. I know Perfect World money, and it is very interesting. I like playing online games. Do you want a try, come and view our website, and you will learn much about cheap Perfect World Gold. Come and join with us. We are waiting for your coming.
By Anonymous, at April 10, 2009 at 11:05 PM
The game has a lot of LOTRO Gold, as long as you join to play this game you will get
them. you should try your best to earn as much Lord Of The Rings Gold as you can, so you are strong and no one can
fight you. you can go to buy LOTRO Gold in the game, so many other players want to play with you together. As long as you have the cheap Lord Of The Rings Gold you will be strong and you can go to kill the monsters to upgrade alone.
I like to earn rappelz rupees, because it is very interesting. I think if you meet the rappelz gold, you will like it too. And when you play the game well you can get some cheap rappelz rupees as the rewards from the game. As long as you have a lot of rappelz money in the game you will be strong, and you can go to buy rupees with the other players.
By Anonymous, at April 11, 2009 at 12:14 AM
buy wow goldAsesor ProfessionalUruguayProfessionalbuy wow goldOfficeLinksNotice
By Anonymous, at April 19, 2009 at 11:07 AM
Do you know the Archlord gold, in the game you need the Archlord money. it can help you increase your level. My friends always asked me how to buy Archlord gold, and I do not know he spend how much money to buy the archlord online Gold, when I see him in order to play the game and search which the place can buy the
cheap Archlord gold. I am happy with him.
Do you know the 2moons dil, in the game you need the 2moons gold. it can help you increase your level. My friends always asked me how to
buy 2moons dil, and I do not know he spend how much money to buy the
2moon dil, when I see him in order to play the game and search which the place can buy the
cheap 2moons gold. I am happy with him.
By Anonymous, at April 26, 2009 at 2:27 AM
Thanks so much.. This makes my week.
By gazeteler, at June 17, 2009 at 2:43 PM
Thank you for sharing to us.there are many person searching about that now they will find enough resources by your post.I would like to join your blog anyway so please continue sharing with us
By Careprost, at March 31, 2011 at 12:33 AM
Post a Comment | Home | Inference: my personal blog