c# - UWP - 当它引用的图像文件发生变化时,图像不会改变内容
问题描述
我正在尝试拍摄视频快照并通过滚动条进行预览。假设我们有bohemian.mp4
,我的应用程序会在某个时候拍摄快照并保存到bohemian.png
. 这里提到的部分。
xml:
<Image x:Name="thumbnail" CacheMode="BitmapCache"
MinHeight="200" MaxHeight="400" Margin="0,20,0,0" Stretch="Uniform"
Source="{x:Bind mp3ViewerViewModel.mp3ViewerModel.ImagePath, Mode=OneWay}"/>
<Slider x:Name="SnapshotSlider"
Minimum="0" Maximum="{x:Bind mp3ViewerViewModel.mp3ViewerModel.Duration}"
Value="{x:Bind mp3ViewerViewModel.mp3ViewerModel.Snapshot}"
IsEnabled="{x:Bind mp3ViewerViewModel.mp3ViewerModel.UseSnapshot, Mode=OneWay}"
/>
<Button x:Name="SaveButton"
RelativePanel.Below="detailGrid" RelativePanel.AlignRightWith="detailGrid"
Foreground="White" BorderBrush="#FF004CFF" Background="#FF0096FF"
Content="Save As"
Command="{x:Bind mp3ViewerViewModel.UpdateSnapshotCmd}"
CommandParameter="{Binding ElementName=SnapshotSlider, Path=Value}"/>
出于调试目的,我会让Button
处理我的Command
查看型号:
public CommandEventHandler<double> UpdateSnapshotCmd
{
get
{
return new CommandEventHandler<double>(
async (second) => await UpdateSnapshotPreview(second)
);
}
}
public async Task UpdateSnapshotPreview(double second)
{
try
{
if (mp4 == null)
{
mp4 = await StorageFile.GetFileFromPathAsync(mp4Path);
}
var imgName = Path.ChangeExtension(mp4.Name, ".png");
StorageFile imgFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(imgName, CreationCollisionOption.ReplaceExisting);
MediaComposition mediaComposition = new MediaComposition();
MediaClip mediaClip = await MediaClip.CreateFromFileAsync(mp4);
mediaComposition.Clips.Add(mediaClip);
var interval = TimeSpan.FromSeconds(second);
var thumbnail = await mediaComposition.GetThumbnailAsync(interval,
1024, 768, VideoFramePrecision.NearestKeyFrame);
using (var randomAccess = await imgFile.OpenAsync(FileAccessMode.ReadWrite))
using (var destStream = randomAccess.AsStream())
using (var sourceStream = thumbnail.AsStream())
{
await sourceStream.CopyToAsync(destStream);
}
mp3ViewerModel.ImagePath = imgFile.Path;
}
catch (Exception ex)
{
await new MessageDialog(ex.Message).ShowAsync();
}
}
模型:
string imagePath = "/Assets/default_thumbnail.jpg";
string defaultImg = "/Assets/default_thumbnail.jpg";
public string ImagePath
{
get
{
if (UseSnapshot)
return imagePath;
else
return defaultImg;
}
set
{
imagePath = value;
this.OnPropertyChanged();
}
}
当我检查保存的文件夹时,png
文件已更改为Slider.Value
但Image
在应用程序中仅响应一次。如何解决这个问题?
解决方案
使用FileSystemWatcher监视文件何时更改:
推荐阅读
- javascript - Vue.js 异步遍历
- kubernetes - Kubernetes Hazelcast 集群中的裂脑发现
- c++ - 从 C++ constexpr 函数调用 C 函数
- batch-file - 在 Apache Tomcat 日志中获取 catalina.out
- sql - SQL Server - 将多个值插入列
- php - XSym 符号链接不能在 PHP on Docker for Windows 中使用
- python - 如何更改标签、行编辑等相对于窗口大小的位置?
- java - 使用 Process builder 从 Java 进程调用的 Perl 脚本挂起(消耗错误,在单独的线程中输出流)
- javascript - 以编程方式浏览 JavaScript 中的嵌套对象
- angular - Angular 6:从组件传递数据并将对象推送到数组中