Reputation: 1
I'm working on a simple UWP C# game and had some issues. I created an Assets folder and filled it with 5 images (0-4.png). I created a class named 'diamond' with this function:
public void insertimage()
{
for (int i = 0; i <= diamond.Length -1; i++)
{
Image img = new Image();
img.Source = new BitmapImage(new Uri($"ms-appx:///Assets/{i}.png"));
diamond[i] = img;
}
}
Now I have an array with 5 images. I created a button on the grid and an IMAGE box. I want that click event to show a random image from the array in the IMAGE box.
Upvotes: 0
Views: 419
Reputation: 5868
If you want to show the image in the image box, you only need to save the BitmapImage into the diamond array. Then when you click the button and get the random index, put the BitmapImage which corresponds to the index to the Source of your image box. For example:
.xaml:
<Grid>
<Image x:Name="myImage" Width="100" Height="100"></Image>
<Button Click="Button_Click">click</Button>
<Grid>
.cs:
public void insertimage()
{
for (int i = 0; i <= diamond.Length -1; i++)
{
BitmapImage bitImg = new BitmapImage(new Uri($"ms-
appx:///Assets/{i}.png"));
diamond[i] = bitImg;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//the index is your random index
BitmapImage pImg = diamond[randomIndex];
myImage.Source = pImg;
}
Upvotes: 0