I have some Image
components on the page and some static images defined in App.xamp. On startup I need to fill Image
components with random static images.
int[] squareImageSources = new int[13];
Random rnd = new Random();
Image[] squareImages = new Image[13];
public Page1()
{
squareImages[0] = i0;
squareImages[1] = i1;
squareImages[2] = i2;
squareImages[3] = i3;
squareImages[4] = i4;
squareImages[5] = i5;
squareImages[6] = i6;
squareImages[7] = i7;
squareImages[8] = i8;
squareImages[9] = i9;
squareImages[10] = i10;
squareImages[11] = i11;
squareImages[12] = i12;
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
int randomNumber = rnd.Next(28);
for (int i = 0; i < 13; i++)
{
while (squareImageSources.Contains(randomNumber))
randomNumber = rnd.Next(28);
squareImageSources[i] = randomNumber;
squareImages[i].Source = (BitmapImage)System.Windows.Application.Current.FindResource("s" + Convert.ToString(randomNumber + 1)); //application closes here
}
}
App.xaml:
<BitmapImage x:Key="s1" UriSource="pack://application:,,,/Resources/Photos/1.png"/>
<BitmapImage x:Key="s2" UriSource="pack://application:,,,/Resources/Photos/2.png"/>
.....................................................
<BitmapImage x:Key="s28" UriSource="pack://application:,,,/Resources/Photos/28.jpg"/>
But application just closes, I recieve no exception. What can be wrong with this code?
UPD:
Tried this way:
try
{
BitmapImage bi = (BitmapImage)System.Windows.Application.Current.TryFindResource("s" + Convert.ToString(randomNumber + 1));
squareImages[i].Source = bi; //nullReferenceException
}
catch
{
}
catch
catches NullReferenceException. How is that possible? I can use this recource in designer and if works fine.