c# - CopyAsync 方法不替换现有文件。UWP。应用存储
问题描述
现在我正在我的应用程序中进行编辑操作。因此,在编辑操作中,可以更改图像,同时保留联系人图像的文件的名称不能重命名(在操作的一个会话期间,图像文件将发生变化,但所有这些文件都将采用相同的名称) .
而且我使用了方法重载(此处CopyAsync
为该方法的文档),据我所知,必须将文件替换为具有相同名称的现有文件。
我得到imageFile
变量FileOpenPicker
。每次我选择 image by 时,下面的代码都会运行FileOpenPicker
。Image
当我在结果UI 控件中重新选择图像时,会显示我之前选择的图像。我希望这Image
将查看我选择的最后一张图片,但这并没有发生。
public BitmapImage Image { set; get; }
//Copy of the file that saved in temporary storage
StorageFile fileForView = await imageFile.CopyAsync
(ApplicationData.Current.TemporaryFolder,
fileName,
NameCollisionOption.ReplaceExisting);
Image = new BitmapImage(new Uri(fileForView.Path));
CopyAsync
如果我在这种情况下,也许我不正确地理解方法的逻辑,如果可能的话,请告诉我如何使我的计划使用这种方法。否则,请提供您的解决方案,因为我现在不知道该怎么做。
我也尝试用CopyAndReplaceAsync
方法来做到这一点。但还是没有结果。我这样做:
if (null != await ApplicationData.Current.TemporaryFolder.TryGetItemAsync(fileName))
{
StorageFile storageFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(fileName);
await storageFile.CopyAndReplaceAsync(imageFile);
}
解决方案
我希望 Image 会查看我选择的最后一张图片,但这并没有发生。
上面的CopyAsync
方法是正确的,我用下面的代码测试过,效果很好。
private async void ListOption_ItemClick(object sender, ItemClickEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
StorageFile fileForView = await file.CopyAsync(ApplicationData.Current.TemporaryFolder, file.Name, NameCollisionOption.ReplaceExisting);
Image = new BitmapImage(new Uri(fileForView.Path));
TestImg.Source = Image;
}
else
{
}
}
推荐阅读
- javascript - 正则表达式 - 可选的非捕获组不匹配
- python - 只能分析第一行数据,其余为 NaN。(Python、熊猫、谷歌 Colab)
- scala - 在scala中获取char编码信息
- sql - 子查询返回超过 1 个值。当子查询遵循 SQL Server 时,这是不允许的
- javascript - 按按钮复制组件
- php - 从laravel中的一句话中取一个词?
- asp.net-core - 如何通过 Swashbuckle 使 SwaggerUI 在 traefik 代理后面的 docker 容器中工作?
- json - 意外字符('-'(代码 45)):soapUI 上的预期空间分隔根级值错误
- python - VSCode Jupyter 在使用 Sympy + IPython.display 时挂断
- node.js - mongodb 中的 Decimal128 格式值有时会给出 NaN 值