首页 > 解决方案 > 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.ValueImage在应用程序中仅响应一次。如何解决这个问题?

标签: c#uwp

解决方案


使用FileSystemWatcher监视文件何时更改:


推荐阅读