I am dynamically adding an image to a canvas object which was working for a while and suddenly stopped displaying for some particular reason.
Here is the code :
public void shootProjectile()
{
var projectileImage = new Image();
var currChar = (CharactersRef.SCharacterProjectile)lstSubMenu.SelectedItem;
projectileImage.Name = currChar.projectileName;
Uri imageUri = new Uri("/Images/Projectiles/"+ currChar.projectileName +".png", UriKind.Relative);
projectileImage.Height = currChar.projectileHeight;
projectileImage.Width=currChar.projectileWidth;
var currPuppet = pg.PuppetList.SingleOrDefault(p => p.puppetID == turnID);
var projectileY = Convert.ToDouble(pcCombatArea.PhysicsObjects[currPuppet.CharacterName+"torso"].GetValue(Canvas.TopProperty));
var projectileX = Convert.ToDouble(pcCombatArea.PhysicsObjects[currPuppet.CharacterName + "torso"].GetValue(Canvas.LeftProperty));
if (currPuppet.faceRight)
{
projectileX += currPuppet.elbowToArmpit + currPuppet.elbowToHandTip;
}
else
{
projectileX -= currPuppet.elbowToArmpit + currPuppet.elbowToHandTip;
}
projectileImage.SetValue(Canvas.LeftProperty,projectileX);
projectileImage.SetValue(Canvas.TopProperty, projectileY);
cnvGame.Children.Add(projectileImage);
projectileImage.Source = new BitmapImage(imageUri);
projectileImage.ImageOpened += new EventHandler<RoutedEventArgs>(projectileImage_ImageOpened);
}
The Top and Left values are well in the range of the canvas size, (The canvas is 800 by 600 whilst the coordinates are round 200, 100). I can find the images in the canvas children, and the url seems to be ok, I double checked it. I set the png image Build type to resource but to no avail. If you notice I have added an ImageOpened even which is not being triggered.