Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Dictionaries in Xaml?
10 replies. Latest Post by Thomas Claudius Huber on June 28, 2009.
(0)
MichaelD...
Member
21 points
35 Posts
07-04-2008 11:11 PM |
Hello!
Does anyone know if it's possible to create Dictionaries in Xaml? I have an object named "NamedResources" that inherits from Dictionary<string,IResource> and I'd like to define a couple items in Xaml. So far I've had no luck doing this.
Any guidance/advice would be appreciated! :)
Thanks,
Michael
swildermuth
Star
8320 points
1,546 Posts
07-05-2008 4:55 AM |
Its possible, but I don't have an example on me. There is a good example in the Sells/Griffiths WPF book though.
CraigN
352 points
89 Posts
07-05-2008 7:10 AM |
I think he wanted to know if he can use the BCL ones. No, they don't exist under Silverlight 2 beta 2. Shaun is suggesting you write your own. I'd love to know why such a useful and well used class never made it into the Silverlight BCL.
If I remember correctly, an alternative lives under the System.ServiceModel namespace named something completely different, which screws up the idea of sharing code between server and client.
07-05-2008 7:08 PM |
Craig, I think you're confused. The Generic Dictionary is definitely in SIlverlight 2. System.Collections.Generic.Dictionary<>. I think he was trying to determine how to instantiate them in XAML. The problem there is that Silverlight doesn't support "TypeArgument" so that you can't create genertic types directly in XAML. You'll have to do it via code.
07-05-2008 7:10 PM |
Here is a blog entry that talks about some work arounds that *may* work in SIlverlight:
http://blogs.msdn.com/mikehillberg/archive/2006/10/06/LimitedGenericsSupportInXaml.aspx
07-05-2008 7:16 PM |
Hmm, I must be confusing it with the SortedDictionary<T>. My bad, apologies.
07-05-2008 8:44 PM |
Yes I muddled around with that a bit.
I'm actually using a closed Dictionary:
public class NamedResourceCollection : Dictionary<string, IResource>
{
}
public
{}
When I do the following:
<Namespace:NamedResourceCollection>
<Namespace:TestResource x:Key="MyKey" />
</Namespace:NamedResourceCollection>
I get the following error: NamedResourceCollection does not support TestResource as content.
I've also made NamedResourceCollection : Dictionary<string,TestResource> and got the same error...
07-06-2008 5:58 AM |
There are two issues here. Dictionaries don't have a common collection (where this syntax would help) because they are two related collections Keys and Values. There is no good way to describe this in XAML. If I change the collection to a List<>, the XAML works:
public interface IResource{}
public class NamedResourceCollection : List<IResource>{}
public class TestResource : IResource{}
Then this works:
<Grid.Resources> <my:NamedResourceCollection x:Key="myList"> <my:TestResource /> </my:NamedResourceCollection></Grid.Resources>
The problem is that if its a dictionary, this doesn't work (becaause there is no way to call "Add" method):
public class NamedResourceCollection : Dictionary<string, IResource>{}
<Grid.Resources> <my:NamedResourceCollection x:Key="myList"> <my:NamedResourceCollection.Values> <my:TestResource /> </my:NamedResourceCollection.Values> </my:NamedResourceCollection></Grid.Resources>
Setting the Values or Keys of the Dictionary separately isn't valid. And there is no way to specify what is the key and what is the value (x:Key is something completely unrelated to Keys/Values of Dictionaries).
HTH
07-06-2008 9:49 AM |
Interesting. According to the previous post, dictionaries are possible in WPF Xaml. So perhaps it's a Silverlight shortcoming/bug. Sigh...
07-07-2008 4:36 AM |
I think I was in error...I don't think Dictionaries specifically (or any collection with multiple Template Parameters) would be supported in XAML in any way, but creating and using these in the code behind works well.
Thomas C...
76 points
20 Posts
06-28-2009 5:15 AM |
Hi there, I came across this thread while having exactly the same (still in Silverlight 3 beta existing) Problem. So, to clarify it here:
In WPF there are two types of collections supported by XAML:IList and IDictionary. The first is a simple list, the second is a collection storing keyValue-Pairs. For IList the Add-Method is simply called. On IDictionary-Collections the x:Key-Attribut must be placed on elements. The value of x:Key is passed in as first parameter to the Add-Method, the Element itself as second parameter.
In Silverlight, there are also two types of collections supported by XAML:IList and ResourceDictionary. Unfortuneately IDictionary seems not to be supported by the XAML-Prozessor of Silverlight (it's a different one than WPFs :-).