KentZhou
KentZhou

Reputation: 25573

How to get refresh of image in Silverlight?

I try to load image dynamically. The XAML is:

The code to load image is: (it is fired by button click event in code behind)

Image myimage = new Image() { Source = new BitmapImage(new Uri(url)) };
this.myPlayer.Content = myimage;

The URL point to a site for the image. Suppose I have image with URL like:

"http://mysite/myfolder/my.jpg"

Then I run the app, click on the button, I can get the image and display it in UI without problem.

Then I override the image file on server with the same name, then click on the button again, the image is not refreshed. I need to reload the whole SL app for the new image display. How to resolve this problem?

Upvotes: 1

Views: 1016

Answers (2)

user1345223
user1345223

Reputation:

Easiest way to resolve that issue is to put a random number at the end of the image URL.

Random random = new Random();
int randomValue = random.Next(1000000);  // will return value between 0 & 1,000,000
string base = "http://mysite/myfolder/my.jpg";
string url = base + "?unused=" + randomValue;
//Note url now equals: "http://mysite/myfolder/my.jpg?unused=####"

Image myimage = new Image() { Source = new BitmapImage(new Uri(url)) };
this.myPlayer.Content = myimage;

This works because the web server will automatically ignore the ?unused= part appended to the image name.

Each time you roll through this function, you should receive a new random value from random.Next(...) which will indicate a "new" image request and will bypass any caching that may be going on.

Upvotes: 1

jv42
jv42

Reputation: 8593

You can specify the caching behavior using the BitmapImage.CacheOption property: http://msdn.microsoft.com/fr-fr/library/system.windows.media.imaging.bitmapimage.cacheoption.aspx

You could try OnLoad before None: http://msdn.microsoft.com/fr-fr/library/system.windows.media.imaging.bitmapcacheoption.aspx

Upvotes: 0

Related Questions