Skip to main content

Microsoft Silverlight

Answered Question Dictionaries in Xaml?RSS Feed

(0)

MichaelDAscentium
MichaelD...

Member

Member

21 points

35 Posts

Dictionaries in Xaml?

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
swildermuth

Star

Star

8320 points

1,546 Posts

Re: Dictionaries in Xaml?

Its possible, but I don't have an example on me. There is a good example in the Sells/Griffiths WPF book though.

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

CraigN
CraigN

Member

Member

352 points

89 Posts

Re: Dictionaries in Xaml?

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.

Microsoft Xbox MVP

swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Re: Re: Dictionaries in Xaml?

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.

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Re: Re: Dictionaries in Xaml?

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

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

CraigN
CraigN

Member

Member

352 points

89 Posts

Re: Re: Dictionaries in Xaml?

Hmm, I must be confusing it with the SortedDictionary<T>. My bad, apologies.

Microsoft Xbox MVP

MichaelDAscentium
MichaelD...

Member

Member

21 points

35 Posts

Re: Re: Dictionaries in Xaml?

Yes I muddled around with that a bit.

I'm actually using a closed Dictionary:

public class NamedResourceCollection : Dictionary<string, IResource>

{

}

 

public class TestResource : IResource

{}

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...

swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Re: Re: Re: Dictionaries in Xaml?

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

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

MichaelDAscentium
MichaelD...

Member

Member

21 points

35 Posts

Re: Re: Re: Re: Dictionaries in Xaml?

Interesting.  According to the previous post, dictionaries are possible in WPF Xaml.  So perhaps it's a Silverlight shortcoming/bug.  Sigh...

swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Answered Question

Re: Re: Re: Re: Re: Dictionaries in Xaml?

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.

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

Thomas Claudius Huber
Thomas C...

Member

Member

76 points

20 Posts

Re: Re: Re: Re: Re: Dictionaries in Xaml?

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 :-).

 

 

 

Thomas Claudius Huber
http://www.thomasclaudiushuber.com
also try http://www.thomasclaudiushuber.com/pageInSilverlight ;-)
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities