Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Woodworking, .NET Programming, CNC, Nature, and other topics.

Pete Brown writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. On most forums, Pete goes by the name Psychlist1972. Pete has worked at Applied Information Sciences (AIS) since 1996 where he currently performs as a lead architect and project manager.

Subscribe to my feed

Add to Technorati Favorites
Applied Information Sciences - My Employer

Community Events



World Domination

who's online

Networks


View Pete Brown's profile on LinkedIn

AddThis Social Bookmark Button

Silverlight 2 Beta 1 Path and File Resolution

How paths are resolved in Silverlight 2 can be somewhat confusing. This has come up a lot in the Silverlight.net forums and elsewhere, so I thought I'd take a stab at putting it all down here. For additional information on Silverlight 2 Beta 1 and how packaging and applications work, see my two part Xap/App posts here: Part 1, Part 2

Images with Leading Slash (like <Image Source="/foo.jpg" />)

This type of references, with a leading slash, is root relative to the application root (the Xap). These files should by Content and have CopyToOutputDirectory set so that they are added into the Xap. If you inspect the Xap, you'll find the image in there.

When not found, the resource loading mechanism falls back to the application's site of origin and looks for the image under the same dir as the Xap is located. Note that this is the application's site, not the hosting page's site. That is an important distinction if you are creating cross-domain applications (where Site X has the page and Site Y has the Xap). If not found, an exception is thrown.

Images without Leading Slash (like <Image Source="foo.jpg" /> )

This type of reference, without any leading slash or anything, expect to find the image compiled into the assembly as a Resource. The path is relative to the path of the Xaml file from which they are being referenced. When not found, the ImageFailed event is raised

If you inspect the Xap, you will not see the image, because it will be in the assembly.

Absolute URLs (like <Image Source="absolute http url" />)

This will look a the named absolute Url as you would expect.

Here's a quick example that shows it:

<UserControl x:Class="PeteBrown.SilverlightPathTest.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="Black">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        
        <!-- Image that is just content, with CopyToOutputDirectory -->
        <Image Source="/Subfolder/image_content.jpg" Grid.Row="0" Grid.Column="0" />
        <TextBlock Text="Content" Foreground="White" Grid.Row="0" Grid.Column="0" />

        <!-- Image that is set as a resource, notice leading slash -->
        <Image Source="Subfolder/image_resource.jpg" Grid.Row="0" Grid.Column="1" />
        <TextBlock Text="Resource" Foreground="White" Grid.Row="0" Grid.Column="1" />

        <!-- Absolute Url -->
        <Image Source="http://www.irritatedvowel.com/pub/blog/SilverlightBalloonsforSilverlight2Beta1_B387/image.png" 
               Grid.Row="0" Grid.Column="2" />
        <TextBlock Text="Absolute" Foreground="White" Grid.Row="0" Grid.Column="2" />

    </Grid>
</UserControl>

Here are the settings in the project for the two files:

image

image

And here is the output:

image

If you look inside the generated Xap file, you'll see the following structure:

  • PeteBrown.SilverlightPathTest.xap
    • AppManifest.xaml
    • PeteBrown.SilverlightPathTest.dll
    • System.Windows.Controls.dll
    • System.Windows.Controls.Extended.dll
    • SubFolder
      • image_content.jpg

Recall that the image_resource.jpg file is compiled as a resource right into the PeteBrown.SilverlightPathTest.dll

Thanks to Ashish Shetty, PM from the Silverlight team, for helping to clear this up for me a while back.

  Add to Technorati Favorites
Posted: Sunday, March 09, 2008 10:10 PM by Pete.Brown
Filed under: , ,

Comments

Michael Sync said:

nice one. thanks..
# March 9, 2008 11:37 PM

Christopher Steen said:

ASP.NET LinkButtons in UpdatePanel cause full postback unless you give them IDs [Via: alexcampbell ]...
# March 10, 2008 2:13 AM

Christopher Steen said:

Link Listing - March 9, 2008
# March 10, 2008 2:13 AM

James Hancock said:

So how would one make a URL relative to a site? I.e. we want to be able to have our clients brand our silverlight stuff, so we want them to be able to change graphics that are loaded. Thus we want to be able to do what we do in HTML: /Images/SomeImage.png where that relates to the currently hosted site's url. With the above example that's not possible. So how would you make it relative without having to hard code the http:// stuff?
# March 10, 2008 4:00 PM

Pete.Brown said:

There is no way to do this in Silverlight 2 Beta 1; you have to use an absolute URL. Now, you can go ahead and pass the site's base URL into the object (as a parameter) when you create the object, but you'll need to code all your image loads instead of relying on the xaml to load them for you.

The other option is not to host the xap yourself, but to let your customer host it on their site.

I sent your feedback to the product team. Others have asked for the same thing. One can only hope that they find a way to provide this functionality.

Pete

# March 11, 2008 8:44 AM

James Hancock said:

Thanks! My suggestion is to use ~/Images/ etc. if you want to make it relative to the site instead of relative to the XAP file... That would solve the entire problem and use a notation that ASP.NET programmers are used to.
# March 11, 2008 10:00 AM

Pete.Brown said:

That was my suggestion too. The problem is, the Silverlight plugin has no way of automatically knowing what the site root is.

Althought that just gave me an idea I might suggest.

Pete

# March 11, 2008 10:22 AM

Chris said:

Please get rid of that annoying silverlight animation on your page.  It's more distracting and worse that most ads.

# March 13, 2008 9:42 AM

Pete.Brown said:

@Chris

lol. Yeah, it was pretty annoying. I considered putting dancing shadow people in there, but in the end, I just toned it way down to some simple fades. :)

Pete

# March 13, 2008 5:32 PM

Community Blogs said:

Bill Reiss posted his SL2 Game Loop part 1, Pete Brown on XAP, SL2 startup, and the Uri Issue, Daniel
# March 14, 2008 7:06 PM

Mark Wisecarver said:

Very good Pete. ;-)
# March 16, 2008 2:32 PM

Mike Taulty's Blog said:

Path and File resolution in Silverlight 2 is confusing so, fortunately, Pete Brown has a good post on...
# March 17, 2008 6:59 AM

Mohamed Mosalem said:

Thanks Pete for this post, i have an issue that i can't solve, i have an html page that contains som Windows Image Acquisition (WIA) scripts that gets an image from the scanner and saves it locally on the client machine, what i did was adding a silverlight control to that page that will allow me to manipulatin the image then later uploading it to the server, the problem i have is that there's no way to make the SilverLight display the local image, i tried but it doesn't work, i was thinking of saving the image inside the isolated storage folder of my silverlight application but then will i be allowed to access it from the silverlight image control??
# March 17, 2008 7:38 AM

Pete.Brown said:

Hi Mohamed

For security reasons, the only way you can open a file like this on the client in Silverlight is by using an OpenFileDialog and then setting the image's source to the resulting stream.

Another option would be for your WIA script to post to the server and download it that way.

However, since you are using Windows-only functionality, I'd recommend looking at using WPF for this, as it has many more features you'll be able to leverage. Silverlight really shines when it comes to cross-browser, cross-platform capabilities.

Pete

# March 17, 2008 10:08 AM

Jason Jung said:

Exactly what I was looking for, Thanks.
# March 17, 2008 7:13 PM

Troy Hall said:

Thanks for the code. It helped solve part of an issue I was having. However, I have another related issue. My SL 2 application dynamically loads a bitmap (PNG or JPEG) into a Canvas that a user has uploaded through a web page. (NB: the bitmap is served via the web server, and is not a resource, and not in the .xap file.) I have tried a couple of methods for loading the image, including using a WebClient (aka Downloader) to retrieve the bitmap stream, and also setting the source using a BitmapImage initialized with the URI of the bitmap. Both methods load the Image. That's not the problem. The problem is, I need to know the dimensions of the image just loaded, as there are some things the app must do right after the image loads. Unfortunately, the Width and Height are always set to Auto (i.e. Double.NaN) right after I set the Image.Source, and are still the same value, even if I try to retrieve them in a BitmapImage.DownloadProgress handler, or in the WebClient.OpenReadCompleted handler. (Actually, in rare cases, the values are correct, indicating some sort of race condition.) In addition, the ActualWidth/Height properties are set to -0- in all cases as well. I have also tried various other hacks to try to force some sort of layout, including toggling the Image.Visibility property. Is there any technique for reliably retrieving the actual width and height of a bitmap loaded either with the WebClient or BitmapImage classes? I am getting desperate. (And, yes, I'd post this in the Silverlight.net forums, but for some reason, it's hosed for me.)
# March 21, 2008 12:05 PM

Ryan Haney said:

What I have found is this: If I load /test.xaml on the fly into a content control, and it has an image control that loads /me.jpg, all is great. But if /test.xaml has a reference to a custom control, that refers to an image, the "relativity" of that image changes (I am guessing to XAP resolution). Interesting!
# April 22, 2008 12:31 AM

Method ~ of ~ failed said:

Skinning an embeddable Silverlight 2 media player
# May 2, 2008 1:40 PM

Tim Heuer said:

While Silverlight 2 brings us great capabilities as .NET developers and opens many opportunities for
# May 2, 2008 1:43 PM

Blogs said:

While Silverlight 2 brings us great capabilities as .NET developers and opens many opportunities for
# May 2, 2008 1:56 PM

Blogs said:

While Silverlight 2 brings us great capabilities as .NET developers and opens many opportunities for
# May 2, 2008 1:58 PM

Tim Heuer said:

While Silverlight 2 brings us great capabilities as .NET developers and opens many opportunities for
# May 2, 2008 2:28 PM

Geoff Hopcraft said:

Is there any reason to choose the resource method over the content method, or vice versa?
# May 6, 2008 3:11 PM

Tick said:

Thanks for the article! Wouldn't have gotten it right as fast without it :)
# May 9, 2008 10:12 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS