Featured post
Why won't my external image load in my Silverlight Page? -
to make simple possible, created new default silverlight 4 application , added images folder test web site automatically created when application generated. put same image, let's call "imagename.png" in clientbin folder , images folder. now, works fine when use image in clientbin using xaml this:
<grid x:name="layoutroot" background="white"> <stackpanel> <image source="imagename.png" width="16" height="16"/> <textbox text="hello" /> </stackpanel> </grid>
but image doesn't load when try access image in images folder this:
<grid x:name="layoutroot" background="white"> <stackpanel> <image source="../images/imagename.png" width="16" height="16"/> <textbox text="hello" /> </stackpanel> </grid>
i don't errors, image isn't there. can tell me what's going on? i've been able find seems suggest should work.
the problem here folder containing xap , top content in xap considered @ root of path. hence path "/" maps top content in xap , folder xap sited (usually clientbin). attempt use parent path past considered root result in silverlight network error.
any content outside of folder xap found in can accessed using absolute uri.
what need convert relative address containing parent path absolute address before passing image.source
property. preferably want avoid hardcoding base absolute address source code.
we can current acquire absolute url xap using baseaddress
property of webclient
object. can compose desired absolute address combining relative address 1 contains parent path.
a value converter can make easier in xaml:-
public class webrelativeconverter : ivalueconverter { public uri base { get; set; } public webrelativeconverter() { base = new uri((new webclient()).baseaddress, urikind.absolute); } public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { uri result = null; if (value uri) { result = new uri(base, (uri)value); } else if (value != null) { result = new uri(base, value.tostring()); } return result; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
now fixed value in xaml this:-
<grid x:name="layoutroot" background="white"> <grid.resources> <local:webrelativeconverter x:key="wrc" /> </grid.resources> <stackpanel> <image datacontext="../images/imagename.png" source="{binding converter={staticresource wrc}}" width="16" height="16"/> <textbox text="hello" /> </stackpanel> </grid>
now if binding source uri or string property of object need add converter , should work.
- Get link
- X
- Other Apps
Comments
Post a Comment