VaSiLiS .
VaSiLiS .

Reputation: 9

Random selection of images at wp7

I have two pages at my project and when i navigate from page1 to page2 i load a picture at a stackpanel using the code below

var imageResource = Application.GetResourceStream(
          new Uri("WindowsPhonePuzzle;component/numbers/numbergame.png",
          UriKind.Relative));   
 this.ImageStream = imageResource.Stream;

if I want to have two or more pictures and load each time a random of them or at a row what I have to change ?

Upvotes: 0

Views: 139

Answers (1)

You'll need to store the Uris of the images you want to use in some kind of Collection. Then use Random to select one.

Random r = new Random(DateTime.Now.Millisecond);
int index = r.Next(0, myCollection.Count);
var imageResource = Application.GetResourceStream(new Uri(myCollection[i], UriKind.Relative)); 
this.ImageStream = imageResource.Stream;

You could also directly access the resources but you'll need to make sure you're dealing with a Uri to an image.

Upvotes: 1

Related Questions