为什么BitmapImage源只能改一次?

Why can change BitmapImage source only once?

拜托,需要帮助。在这段代码中必须是愚蠢的错误,但它是什么?如果我 运行 这个非常简单的代码,我只能加载和更改位图图像一次。第一次 运行 没问题,但如果我想再次更改图像(按下按钮),第一张图像仍然存在。为什么?

XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <Button x:Name="AddProfilePicture"  HorizontalAlignment="Center" Click="AddProfilePicture_Click">
            <Grid Width="200" Height="200">
                <Ellipse Width="200" Height="200">
                    <Ellipse.Fill>
                        <ImageBrush x:Name="ImageBrush_ProfilePicture" Stretch="UniformToFill"/>
                    </Ellipse.Fill>
                </Ellipse>
            </Grid>
        </Button>
    </StackPanel>
</Grid>

代码

    private async void AddProfilePicture_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker profilePictureFilePicker = new FileOpenPicker();
        profilePictureFilePicker.ViewMode = PickerViewMode.Thumbnail;
        profilePictureFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        profilePictureFilePicker.FileTypeFilter.Add(".jpg");
        StorageFile userSelectedProfilePicture = await profilePictureFilePicker.PickSingleFileAsync();
        //MAKE SURE U HAVE THIS FILE (ProfilePicture.jpg) IN FOLDER ALREADY....
        StorageFile destinationFileImage = await StorageFile.GetFileFromPathAsync(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
        await userSelectedProfilePicture.CopyAndReplaceAsync(destinationFileImage);
        Uri profilePictureBitmapURI = new Uri(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
        BitmapImage profilePictureBitmap = new BitmapImage(profilePictureBitmapURI);
        ImageBrush_ProfilePicture.ImageSource = profilePictureBitmap;
    }

此代码没有任何额外检查。这意味着图像 'ProfilePicture.jpg' 必须位于 运行ning 应用程序之前的文件夹中。代码一开始运行良好 运行 但在第二个 运行 (再次按下按钮并选择新图片)即使文件夹中的源更改也无法更改屏幕上的输出。

尝试添加 Load() 方法,看看是否有帮助:

 private async void AddProfilePicture_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker profilePictureFilePicker = new FileOpenPicker();
    profilePictureFilePicker.ViewMode = PickerViewMode.Thumbnail;
    profilePictureFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    profilePictureFilePicker.FileTypeFilter.Add(".jpg");
    StorageFile userSelectedProfilePicture = await profilePictureFilePicker.PickSingleFileAsync();
    //MAKE SURE U HAVE THIS FILE (ProfilePicture.jpg) IN FOLDER ALREADY....
    StorageFile destinationFileImage = await StorageFile.GetFileFromPathAsync(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
    await userSelectedProfilePicture.CopyAndReplaceAsync(destinationFileImage);
    Uri profilePictureBitmapURI = new Uri(ApplicationData.Current.LocalFolder.Path + @"\" + "ProfilePicture.jpg");
    BitmapImage profilePictureBitmap = new BitmapImage(profilePictureBitmapURI);
    ImageBrush_ProfilePicture.ImageSource = profilePictureBitmap;
    ImageBrush_ProfilePicture.Load()
}

在创建 BitmapImage 时将 CreateOptions 设置为 IgnoreImageCache

var profilePictureBitmap = new BitmapImage(profilePictureBitmapURI) {CreateOptions = BitmapCreateOptions.IgnoreImageCache};