在silverlight中动态生成图像的问题

时间:2009-05-03 09:01:17

标签: wpf silverlight image

我正在动画地将一个图像添加到画布对象中,该画布对象已经工作了一段时间并且由于某种特殊原因而突然停止显示。

以下是代码:

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);
}

“上”和“左”值很好地位于画布大小的范围内(画布为800 x 600,而坐标为200,100)。我可以在画布子中找到图像,并且网址似乎没问题,我仔细检查了一下。我将png图像构建类型设置为资源但无济于事。如果你注意到我添加了一个甚至没有被触发的ImageOpened。

1 个答案:

答案 0 :(得分:1)

如果您将图像构建类型设置为“资源”,请尝试从图像URI中删除前导“/”。