Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to list keys within ResourceDictionary? RSS

8 replies

Last post Jul 28, 2009 11:02 PM by John Leitch

(0)
  • riazb

    riazb

    Member

    2 Points

    2 Posts

    How to list keys within ResourceDictionary?

    Apr 24, 2008 03:20 PM | LINK

    I'm having to convert a WPF project to Silverlight 2 and am having an absolute nightmare to come to terms with the much limited Resources in Silverlight (no merge etc).

    So far, I've done the following:

    1) Keep the styling as a ResourceDictionary as an XML data type in SQL Server
    2) Pull the data via WCF to a string
    3) Load the string via XamlReader.Load in Silverlight and cast as ResourceDictionary

    So far, so good. I have a working ResourceDictionary object and all x:Key defined in it.

    As Application.Current.Resources is read-only (unlike WPF), can't simply stick the dynamicly rendered resource dictionary to it.

    Can't also simply do Application.Current.Resources.Add(the_loaded_resource).
    The keys don't get added for some reason.

    What I CAN do is:
    foreach (DependencyObject d in the_loaded_resource)
    {
    Application.Current.Resources.Add(d);
    }

    The problem with the above approach is that 'd' gets added without it's 'key' setting. So while the styles/brushes/etc are added to the Application's resource dictionary, the keys are lost.

    To make things work, I have to manually type the key. Hence
    Application.Curent.Resources.Add("HardCodedKey",d);

    How do I list the keys in the loaded resource dictionary to get all the key names and not having to add them via hard coding?

    Thanks

  • riazb

    riazb

    Member

    2 Points

    2 Posts

    Re: How to list keys within ResourceDictionary?

    Apr 25, 2008 09:03 AM | LINK

    I take it it's not possible then?
  • Yi-Lun Luo - MSFT

    Yi-Lun Luo -...

    All-Star

    25149 Points

    2759 Posts

    Microsoft

    Re: How to list keys within ResourceDictionary?

    Apr 28, 2008 06:35 AM | LINK

    Unfortunately, you can't find all keys in a Silverlight ResourceDictionary. This feature is not implemented yet.

    shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
  • dafatdude

    dafatdude

    Member

    40 Points

    16 Posts

    Re: How to list keys within ResourceDictionary?

    Oct 09, 2008 07:52 AM | LINK

    Yi-Lun Luo - MSFT

    Unfortunately, you can't find all keys in a Silverlight ResourceDictionary. This feature is not implemented yet.

    Thanks for the post.

    I tried this today in RC0 and had the same result. There is still no way to iterate through ResourceDictionary.Values.

    Is this going to be included in the release version, because it's a serious limitation when trying to access resources from dynamically loaded XAML.

    ResourceDictionary not implemented

    If I've answered your query, please mark "Answered"
  • codebased

    codebased

    Participant

    826 Points

    386 Posts

    Re: How to list keys within ResourceDictionary?

    Oct 09, 2008 08:02 AM | LINK

     

    Well at present whatever I add into the resource, I just keep track in my Dictionary collection through this way it is easy to know if it has been loaded etc....

     

     

    "If I've answered your query then please mark it as "Answered".
  • dafatdude

    dafatdude

    Member

    40 Points

    16 Posts

    Re: How to list keys within ResourceDictionary?

    Oct 09, 2008 08:07 AM | LINK

    Our problem is a bit different.

    We're loading some XAML into a UIElement. This XAML has storyboards in it we want to start and stop in code.

    The problem is that while we can go

    (Resources["storyAnimation"] as Storyboard).Start();

    We still need to know the name of the storyboard. The XAML may have a number of storyboards which need to be controlled and the name could be anything.

    If I've answered your query, please mark "Answered"
  • codebased

    codebased

    Participant

    826 Points

    386 Posts

    Re: How to list keys within ResourceDictionary?

    Oct 09, 2008 08:10 AM | LINK

    Umm right, may be you need to use  VisualTreeHelper class to iterate throughout the child recursively and check if it is a story board then do ... else don't doo...

    "If I've answered your query then please mark it as "Answered".
  • sudheeshwarrier

    sudheeshwarrier

    Member

    68 Points

    47 Posts

    Re: Re: How to list keys within ResourceDictionary?

    Mar 05, 2009 03:15 AM | LINK

    is there a solution for this problem????

    Sudheesh Warrier,
    Keep it Simple...
    One stop for all reviews | AtomViews
  • John Leitch

    John Leitch

    Member

    2 Points

    1 Post

    Re: How to list keys within ResourceDictionary?

    Jul 28, 2009 11:02 PM | LINK

    This is actually possible using reflection.

    ResourceDictionary dictionary = new ResourceDictionary()
    {
        { "a", "" },
        { "b", "" },
        { "c", "" }
    };

    PropertyInfo prop = typeof(ResourceDictionary).GetProperty("Keys");
    object[] keys = prop.GetValue(dictionary, null) as object[];

    foreach (object o in keys)
        MessageBox.Show(o.ToString());

    silverlight Reflection solution silverlight Solved WPF/E Solution