DCSIMG
Silverlight Tip: Enumerating embedded resources - Alex Golesh's Blog About Silverlight Development

Silverlight Tip: Enumerating embedded resources

Today I’ve got a “Call for Help” from one of Silverlight MVPs – Bill Reiss. The mail said:

“I would like to enumerate all items that were marked as Content or Resource in the main XAP file.

If I can’t do both I’d settle for one. Any sample code for this would be greatly appreciated.

Bill”

 

I decided to help with embedded resources, because I did something like this in WPF.

I’ve created sample application, which will display a list of resources in list box – from here it is very easy to use them by name. My sample will load the resource as image.

For application I’ve added 3 sample images and marked them “Resource” in properties:

image

The application UI:

image

XAML for sample:

    <StackPanel x:Name="LayoutRoot" Background="White" Orientation="Vertical">
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button Content="Embedded Resources" Padding="5" Margin="5" x:Name="btnEmbedded" Click="btnEmbedded_Click"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <StackPanel Orientation="Vertical" Margin="5">
                <TextBlock Text="Embedded Resources:" Margin="5,0"/>
                <ListBox x:Name="lstEmbedded" SelectionChanged="lstEmbedded_SelectionChanged" Margin="5"/>
                <Image x:Name="imgEmbedded" Stretch="Uniform" Width="150" Height="150" Margin="5,0"/>
            </StackPanel>
        </StackPanel>
    </StackPanel

At code behind I have 2 event handler functions. First – to handle button click and get the resources.

First of all I have to open XAP file as a resource:

WebClient wc = new WebClient();
wc.OpenReadCompleted += (s, args) =>
{
   if (args.Error == null)
   {
      //rest will be here
   }
};
wc.OpenReadAsync(new Uri("SL_Resources_Test.xap", UriKind.RelativeOrAbsolute));

When the OpenRead completes, first I have to get the list of assembly parts in XAP using application manifest.

XmlReader reader = XmlReader.Create(Application.GetResourceStream(new StreamResourceInfo(args.Result, null), 
new Uri("AppManifest.xaml", UriKind.Relative)).Stream); AssemblyPartCollection parts = new AssemblyPartCollection(); if (reader.Read()) { reader.ReadStartElement(); if (reader.ReadToNextSibling("Deployment.Parts")) { while (reader.ReadToFollowing("AssemblyPart")) { parts.Add(new AssemblyPart() { Source = reader.GetAttribute("Source") }); } } }

Now, when I have a list of all parts (DLLs which could have embedded resources) I will get resources from each one of the DLLs:

List<string> embeddedResources = new List<string>(); //Will use this list as source for ListBox later in code

foreach (var part in parts)
{
   Assembly assy = part.Load(Application.GetResourceStream(new StreamResourceInfo(args.Result, null), 
new Uri(part.Source, UriKind.Relative)).Stream); //Load current assembly string[] resources = assy.GetManifestResourceNames(); //Get embedded resource names foreach (var resource in resources) { ResourceManager rm = new ResourceManager(resource.Replace(".resources",""), assy); //All resources has “.resources” in the name – so I have to get rid of it Stream DUMMY = rm.GetStream("app.xaml"); //Seems like some issue here, but without getting any real stream next statement doesn't work.... ResourceSet rs = rm.GetResourceSet(Thread.CurrentThread.CurrentUICulture, false, true); //to get the actual values - create the table //Dictionary<string, object> table = new Dictionary<string, object>(); IDictionaryEnumerator enumerator = rs.GetEnumerator(); while (enumerator.MoveNext()) { embeddedResources.Add((string)enumerator.Key); //It is also possible to get the value and key instead of just key //object obj2 = enumerator.Value; //table.Add((string)enumerator.Key, obj2); } } } lstEmbedded.ItemsSource = embeddedResources; //Use prepared list as a source for ListBox

Now my embedded resources enumerated.

Second part – use the resource by name when selection changed in the list box:

private void lstEmbedded_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Uri uri = new Uri(lstEmbedded.SelectedItem.ToString(), UriKind.RelativeOrAbsolute);

    BitmapImage img = new BitmapImage(uri);
    imgEmbedded.Source = img; 
}

 

Running application:

image

image image image

 

Sample source here.

 

Stay tuned for more posts and news to come from PDC09.

 

 

See you at PDC09 and SDP!

Alex

Published Friday, November 13, 2009 12:28 PM by Alex Golesh

Comments

# Silverlight Tip: Enumerating embedded resources - DevCorner iSilverlight

Pingback from  Silverlight Tip: Enumerating embedded resources - DevCorner iSilverlight

# Silverlight Tip: Enumerating embedded resources - DevCorner Silverlight Blog

Pingback from  Silverlight Tip: Enumerating embedded resources - DevCorner Silverlight Blog

# Silverlight Tip: Enumerating embedded resources - DevCorner Silverlight Web

Pingback from  Silverlight Tip: Enumerating embedded resources - DevCorner Silverlight Web

# Silverlight Tip: Enumerating embedded resources - DevCorner Silverlight Web

Pingback from  Silverlight Tip: Enumerating embedded resources - DevCorner Silverlight Web

# Twitter Trackbacks for Silverlight Tip: Enumerating embedded resources - DevCorner [microsoft.co.il] on Topsy.com

Pingback from  Twitter Trackbacks for                 Silverlight Tip: Enumerating embedded resources - DevCorner         [microsoft.co.il]        on Topsy.com

# re: Silverlight Tip: Enumerating embedded resources

Were you ever going to push the source? This article interested me but I wasn't able to implement my own take based on the snippets above.

I'd really appreciate it. It's a neat idea.

Tuesday, November 24, 2009 1:15 AM by joe

# re: Silverlight Tip: Enumerating embedded resources

Sample posted! Sorry for delay:)

Tuesday, November 24, 2009 8:52 AM by Alex Golesh

# re: Silverlight Tip: Enumerating embedded resources

Sometimes it's really that simple, isn't it? I feel a little stupid for not thinking of this myself/earlier, though.

Thursday, December 31, 2009 6:02 PM by Roulett Trick

# re: Silverlight Tip: Enumerating embedded resources

Damn, that sound's so easy if you think about it.

Thursday, December 31, 2009 6:23 PM by Roulett Tip

# re: Silverlight Tip: Enumerating embedded resources

Great idea, but will this work over the long run?

Thursday, December 31, 2009 6:24 PM by online roulette

# re: Silverlight Tip: Enumerating embedded resources

Great idea, thanks for this tip!

Saturday, January 02, 2010 3:49 AM by Roulette Spielen

# re: Silverlight Tip: Enumerating embedded resources

Damn, that sound's so easy if you think about it.

Saturday, January 02, 2010 3:54 AM by jetzt roulette spielen

# re: Silverlight Tip: Enumerating embedded resources

Hey, ok, I get it, I guess - but does this really work?

Saturday, January 02, 2010 7:29 PM by Online Roulette

# re: Silverlight Tip: Enumerating embedded resources

This brings me to an idea:...

Tuesday, January 12, 2010 11:35 PM by Tip Tricks

# re: Silverlight Tip: Enumerating embedded resources

This brings me to an idea:...

Tuesday, January 12, 2010 11:38 PM by www.roulettetricks.org

# re: Silverlight Tip: Enumerating embedded resources

This brings me to an idea:...

# re: Silverlight Tip: Enumerating embedded resources

This brings me to an idea:...

# re: Silverlight Tip: Enumerating embedded resources

Great idea, but will this work over the long run?

# Enumerating Resources in Silverlight

Pingback from  Enumerating Resources in Silverlight

Thursday, May 06, 2010 1:25 AM by Enumerating Resources in Silverlight

# re: Silverlight Tip: Enumerating embedded resources

Da frage ich mich beim   Durchlesen von blogs.microsoft.co.il ja schon, ob man selbst nicht komplett auf den Kopf gefallen ist. Herzlichen Dank fur deine Erlauterungen          

Friday, February 04, 2011 6:28 AM by Tagesgeld

# re: Silverlight Tip: Enumerating embedded resources

Silverlight tip enumerating embedded resources.. WTF? :)

Monday, April 04, 2011 4:33 AM by blogs.microsoft.co.il

# re: Silverlight Tip: Enumerating embedded resources

Silverlight tip enumerating embedded resources.. I like it :)

Friday, April 22, 2011 4:02 PM by blogs.microsoft.co.il

# re: Silverlight Tip: Enumerating embedded resources

Monument to the author posed for this:)

Wednesday, April 27, 2011 1:16 PM by E Cigarette

# re: Silverlight Tip: Enumerating embedded resources

Super! All would be well written:)

Saturday, May 07, 2011 10:01 PM by goldfishka

# re: Silverlight Tip: Enumerating embedded resources

Hooray!, The one who wrote nishtyak wrote!

Saturday, May 28, 2011 7:47 AM by zhenskayaobuv

# Tips for programmer

Pingback from  Tips for programmer

Saturday, March 10, 2012 2:21 AM by Tips for programmer

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: