C# 为什么只能更改BitmapImage源一次?
求求你,需要帮助。在这段代码中一定是愚蠢的错误,但它是什么?如果我运行这个非常简单的代码,我只能加载和更改位图图像一次。第一次运行正常,但如果我想再次更改图像(按下按钮),第一个图像将保留。为什么? XAML 此代码没有任何额外的检查。这意味着在运行应用程序之前,图像“ProfilePicture.jpg”必须位于文件夹中。代码在第一次运行时运行良好,但在第二次运行时(再次按下按钮并选择“新建图片”)无法更改屏幕上的输出,即使文件夹中的源代码发生了更改。尝试添加Load()方法,看看这是否有帮助:C# 为什么只能更改BitmapImage源一次?,c#,windows-10-universal,bitmapimage,storagefile,fileopenpicker,C#,Windows 10 Universal,Bitmapimage,Storagefile,Fileopenpicker,求求你,需要帮助。在这段代码中一定是愚蠢的错误,但它是什么?如果我运行这个非常简单的代码,我只能加载和更改位图图像一次。第一次运行正常,但如果我想再次更改图像(按下按钮),第一个图像将保留。为什么? XAML 此代码没有任何额外的检查。这意味着在运行应用程序之前,图像“ProfilePicture.jpg”必须位于文件夹中。代码在第一次运行时运行良好,但在第二次运行时(再次按下按钮并选择“新建图片”)无法更改屏幕上的输出,即使文件夹中的源代码发生了更改。尝试添加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()
}
创建位图图像时将创建选项设置为忽略图像缓存
var profilePictureBitmap = new BitmapImage(profilePictureBitmapURI) {CreateOptions = BitmapCreateOptions.IgnoreImageCache};
替代方法:更改椭圆。在替换文件之前填充为null,然后用新文件创建新笔刷。这确实解决了问题!我猜是这样的,但根本不知道怎么做。(甚至尝试等待任务。延迟(5000)…哈)谢谢!PS.尝试更改椭圆ect。设置为null,但这些设置没有任何帮助:-)“在替换文件之前。”我想这是因为
.copyandresplaceasync()
它以某种方式确定映像不再存在,所以它在缓存中使用了一个映像。
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()
}
var profilePictureBitmap = new BitmapImage(profilePictureBitmapURI) {CreateOptions = BitmapCreateOptions.IgnoreImageCache};