Geeks With Blogs
Imran Shaik Silverlight Quintessential Rambling

Silverlight 2.0 Beta 1 is inconsistent with Uri Relative.

Problem:

In designing phase when you give URI relative address to MediaElement/Image or set any source from the code behind, the relative address is with the xaml/vb/cs file you are defining it in, for example, consider the following project

2008-03-06_165455

In Page.xaml if you define the source, the preview shows the image, but when the application is run it doesn't show the image

<Image x:Name="bg" Source="bg.jpg"/>

Reason:

The reason is the relative uri for the source, however the preview works but when complied the "xap" is generated in ClientBin directory, and the relative Uri will be changed to ClientBin so in run time it will look for the bg.jpg in ClientBin directory.

Solution:

Solution 1: Solution is the provide absolute Uri for the source, eg.,

<Image x:Name="bg" Source="http://website.com/bg.jpg"/>

Solution 2: Copy all your assets in ClientBin directory

Solution 3: Use absolute uri if setting from code behind eg.,

<MediaElement x:Name="ThisVideo" Height="500" />

Now set the source from code behind using Application path, in VB (take out _  and add ; at the end for C#),

VideoScreen.Source = _
 New Uri(Application.Current.Host.Source.AbsolutePath & _ 
"../../../video.wmv", UriKind.Absolute)

Please leave your comments.

 
Posted on Thursday, March 6, 2008 5:14 PM | Back to top


Comments on this post: Silverlight 2.0 Beta 1 Uri inconsistency

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
The following code doesn't show the image.

<Image x:Name="myImage" Source="http://localhost/2.jpg" Visibility="Collapsed"></Image>

The image is in the website folder and not in the Silverlight project folder. What am I doing wrong? Also, how do I set it so that it shows ad design time too? Do I have to copy it over to my Silverlight folder?
Left by yaip on Mar 09, 2008 7:04 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
I was helpful, thanks.
Left by Jacek Ciereszko on Mar 26, 2008 12:43 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
What about change image/video's properties "Copy to Output Directory" to "Copy Always"? Then your image is always moved to ClientBin folder and application can see it. No need to use "Absolute" path.

So, from my view, we have 2 way to do this:
1. as a resources
2. copy image/video to ClientBin
both works
Left by Jacek Ciereszko on Mar 27, 2008 11:16 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Hi Jacek,

I know you can do that but there are drawbacks with doing that first if you mean embedded resources then they are going to be downloaded along with the XAP file, weather or not that asset it used in the application or not, wasting a bit of server bandwidth.

Second "Copy to Output Directory", well that is the simple way of doing it but if your assets are particularly huge it would be awful waste of server storage, espcially when you already it available outside the ClientBin Directory.

The trick was to just use the resources already in place to just point to it, insted of wasting server bandwidth/storage.
Left by Imran on Mar 28, 2008 12:19 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Hi,
I really want to thank u.
now i am able to set image source at .xaml page.
but still i have problem how to set image source at page.xaml.cs page.
Left by manish kumar on Apr 09, 2008 7:34 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Hi,
I am not able to set the image source to Image control in XAML cs file. I am getting compile time error when i set the Source for the image control

I have placed the image in hosting project folder and not in Silverlight project folder.

My code is:
string path = HtmlPage.Document.DocumentUri.AbsolutePath;
path = path.Substring(0, path.LastIndexOf("/") + 1);
return string.Concat("http://", HtmlPage.Document.DocumentUri.Host, ":", HtmlPage.Document.DocumentUri.Port, path);
string strImagePath = GetAppPath() + "Images/msdn_logo_small.png";
imgLogo.Source =new Uri(strImagePath, UriKind.Absolute);
Left by parimal on Apr 09, 2008 8:44 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Hi Parimal/Manish,

In Silverlight 2.0 Image source from managed code should be set using BitmapImage, check the syntax on MSDN.

The Syntax should be something like,

imgLogo.Source = New Media.Imaging.BitmapImage(new URI(strImagePath, UriKind.Absolute));

Or you can also use the ImageSource class. Please check MSDN documentation.

Left by Imran on Apr 09, 2008 9:00 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...

Thanks Imran..

I took help from MSDN and was successful in the implementation.

Image img = new Image();
System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();

string strImagePath = GetAppPath() + "Images/msdn_logo_small.png";
bi.UriSource = new Uri(strImagePath, UriKind.Absolute);
imgLogo.Source = bi;

GetAppPath Method
public string GetAppPath()
{
string st1=string.Empty;
try
{
string path = HtmlPage.Document.DocumentUri.AbsolutePath;
path = path.Substring(0, path.LastIndexOf("/") + 1);
st1= string.Concat("http://", HtmlPage.Document.DocumentUri.Host, ":", HtmlPage.Document.DocumentUri.Port, path);
}
catch (Exception ex)
{
WriteError(ex);
}
return st1;
}
Left by parimal on Apr 09, 2008 10:55 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
All I had to do for a solution was to change the image/video properties, Build Action, to "Resource" ... then it worked fine.
Left by Mark on Jun 27, 2008 10:07 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Thanx You.. Perfect Docs
Left by Turkey on Jul 31, 2008 6:51 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
You, sir, deserve a cookie.

I have been banging my head on Microsoft's documentation which clearly states "The starting location for the relative reference is the HTML page that contains the current Silverlight plug-in."

Thank you for pointing out the truth about the relative path.
Left by jeffa on Aug 14, 2008 2:37 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
i like the things which you need ,like so much
Left by cheap nike shoes on Mar 30, 2009 6:26 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
i will bookmark your website, it is cool
Left by cheap chanel handbags on Mar 30, 2009 6:31 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
thanks docs perfect you
Left by tours on Sep 12, 2009 8:55 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
thanks documentss and excellent you
Left by tour on Sep 12, 2009 8:58 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Great tutorial, thanks.
Left by 1000 games on Oct 06, 2009 6:25 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
i like this is website
Left by cheap gucci handbags on Oct 10, 2009 11:50 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
It is a great post ,thanks for your share.
Left by mbt on Jun 29, 2010 7:08 PM

# fggdfg
Requesting Gravatar...
Left by cheap handbags on Sep 01, 2010 8:31 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Nice Coding wanted to say thanks since I'm just getting started with Silverlight
Left by Sandy on Jan 16, 2011 3:56 AM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Do not be surprised if you want to keep coach handbagfor yourself.There is bcoach outlet in our shops, you can choose them in Internet.cheap coach handbags are a requirement when you go out.coach outlet looks great in pictures, and when you feel it in your hands, you can actually see coach bag is really a quality product.
Left by coach bag on Jan 27, 2011 7:33 PM

# re: Silverlight 2.0 Beta 1 Uri inconsistency
Requesting Gravatar...
Thanks in advance for your help
Left by Soccer Jerseys on Apr 23, 2011 8:36 PM

Your comment:
 (will show your gravatar)


Copyright © Imran Shaik | Powered by: GeeksWithBlogs.net