UWP 图片不是 updating/reloading

UWP Image not updating/reloading

我知道在 SO 上有很多类似的问题,但是 none 到目前为止已经解决了这个问题或展示了一个有效的解决方案。

在 UWP 应用程序中,我有一个 Image(在 ViewBox 内),其来源因用户选择新图像而改变。我已经确认代码中的任何地方都没有抛出异常,并且下面的 switch-case 中的所有内容都可以正常工作。我试过在图像上使用 InvalidateArrange()InvalidateMeasure()UpdateLayout(),在 ViewBox 上使用 UpdateLayout(),但似乎没有任何效果。代码运行时会删除初始图像,但不会显示新图像,它只会显示空白 space.

有人可以查看下面的代码,看看是否可以找到问题所在吗?我确定我忽略了一些简单的事情,但我似乎无法找到它是什么。

C#代码

private async void ChangeIcon(int selection)
{
    try
    {
        switch (selection)
        {
            case 0:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpRed.png"));
                break;
            case 1:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpOrange.png"));
                break;
            case 2:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpYellow.png"));
                break;
            case 3:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpGreen.png"));
                break;
            case 4:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpBlue.png"));
                break;
            case 5:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpPurple.png"));
                break;
            case 6:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/tpPink.png"));
                break;
            case 7:
                imageEntry.Source = new BitmapImage(new Uri("ms-appx:///FtpSharp/Assets/ftpTeal.png"));
                break;
        }
    }
    catch (Exception ex)
    {
        ContentDialog dialog = new ContentDialog
        {
            Title = "Exception found!",
            Content = ex.ToString(),
            CloseButtonText = "Understood"
        };
        await dialog.ShowAsync();
    }           
}

ViewBox/Image XAML

<Viewbox Grid.Column="2" Grid.Row="0" Grid.RowSpan="4" Margin="5,15,5,0">
    <Image x:Name="imageEntry" Source="Assets/SquircleX.png" Tapped="ImageEntry_TappedAsync" />
</Viewbox>

问题出在您使用的 URI - 删除 FtpSharp 部分,例如:

ms-appx:///Assets/ftpRed.png

原因是ms-apps:///已经指向你应用程序安装文件夹的根目录,所以Assets文件夹将直接部署在那里。