Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating Image Dynamically
    primarykey
    data
    text
    <p>I am having a problem while making a Whack a Mole type game, I am trying to create the images where the mole will appear dynamically, yet there is only a blank white screen where the stack panel is. It is fair to say that I am a noob.</p> <p>This is my loop where I am trying to create these images:</p> <pre><code> Image[] ImageArray = new Image[50]; InitializeComponent(); //string ImageName = "Image"; for (int i = 0; i &lt;= 8; i++) { Image Image = new Image(); ImageArray[i] = Image; Image.Name = "Image" + i.ToString(); StackPanel1.Children.Add(ImageArray[i]); } //Random Number Generator Random rnd = new Random(); int num = rnd.Next(1, 9); //If Random Number is "1" Then Image will display if (num == 1) { ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole)); ImageArray[1].Source = MoleImage; } </code></pre> <p>This is the StackPanel XAML:</p> <pre><code> &lt;Window x:Name="Window1" x:Class="WhackaMole.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="468.843" Width="666.045" OpacityMask="#FFF70D0D" Icon="mole2.png" Cursor="" &gt; &lt;Grid OpacityMask="#FF5D1313"&gt; &lt;Image Margin="422,191,-185,-69" Source="mole2.png" Stretch="Fill" HorizontalAlignment="Right" VerticalAlignment="Bottom"/&gt; &lt;TextBlock HorizontalAlignment="Left" Margin="35,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="52" Width="595" FontSize="50" FontFamily="SimHei"&gt;&lt;Run Language="en-ca" Text="Can You Catch the Mole?"/&gt;&lt;/TextBlock&gt; &lt;Button x:Name="NewGameBttn" Content="New Game" HorizontalAlignment="Left" Margin="77,0,0,16" VerticalAlignment="Bottom" Width="139" Height="50" FontSize="25" Click="NewGameBttn_Click"/&gt; &lt;Button x:Name="CloseBttn" Content="Close" HorizontalAlignment="Left" Margin="245,365,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="29" Click="CloseBttn_Click"/&gt; &lt;StackPanel x:Name="StackPanel1" HorizontalAlignment="Left" Height="231" Margin="35,112,0,0" VerticalAlignment="Top" Width="525"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload