我有以下XAML代码:

 <Image x:Name="armImage" Source="{Binding PlayerImage}">

        </Image>

这是幕后发生的事情。我有这个属性:
private BitmapImage playerImage;
public BitmapImage PlayerImage
{
    get { return playerImage; }
    set
    {
        this.playerImage = value;
        this.PropertyChanged(this, new PropertyChangedEventArgs("PlayerImage"));
    }
}

我这样设置:
private void GameStarted(object sender, EventArgs.GameStartedEventArgs e)
{
    if (e.IsUIHidden)
    {
        MainPlayer = new Player(0, new Uri("/Images/arm.bmp", UriKind.Relative));
        this.PlayerImage = DisplayImage(MainPlayer.ImageUri);

    }
}

DisplayImage方法如下所示:
private BitmapImage DisplayImage(Uri imageUri)
   {
if (imageUri != null)
{
    return new BitmapImage(imageUri);
}
else
{
    throw new InvalidOperationException();
}
   }

问题如下-当我设置属性PlayerImage时,UI中的图像没有改变。我尝试在没有MVVM模式的情况下进行操作,并且Uri正常工作并显示了图像,但是当我尝试通过这种方式进行操作时不起作用?

最佳答案

您的媒体资源名称是PlayerImage而不是PlayerImageUriPropertyChanged调用中进行了更改

this.PropertyChanged(this, new PropertyChangedEventArgs("PlayerImageUri"));

应该
this.PropertyChanged(this, new PropertyChangedEventArgs("PlayerImage"));

10-04 12:16