Wpf 图像源不会从第二次更新
我面临一个特殊的问题,当源代码从第二次修改时,图像不会更新 我的xaml和代码隐藏Wpf 图像源不会从第二次更新,wpf,image,Wpf,Image,我面临一个特殊的问题,当源代码从第二次修改时,图像不会更新 我的xaml和代码隐藏 <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="140"/> </Grid.RowDefinitions> <Grid Grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="140"/>
</Grid.RowDefinitions>
<Grid Grid.Row="2" Margin="0 55 0 0" x:Name="ImageGrid" >
</Grid>
</Grid>
FileSystemWatcher myWatcher;
Dispatcher myDisp;
private int count = 0;
public MainWindow()
{
InitializeComponent();
myWatcher = new FileSystemWatcher();
myWatcher.Path = @"C:\Test";
myWatcher.Filter = "hospital.png";
myWatcher.NotifyFilter = NotifyFilters.LastWrite;
myWatcher.Changed += myWatcher_Changed;
myWatcher.EnableRaisingEvents = true;
myDisp = Dispatcher.CurrentDispatcher;
}
void myWatcher_Changed(object sender, FileSystemEventArgs e)
{
myWatcher.EnableRaisingEvents = false;
string aImgPath = @"C:\Test\hospital.png";
if (File.Exists(aImgPath))
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(aImgPath, UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
src.Freeze();
myDisp.Invoke(DispatcherPriority.Normal, new Action<BitmapImage>(UpdateImage), src);
}
myWatcher.EnableRaisingEvents = true;
}
void UpdateImage(BitmapImage theImage)
{
ImageGrid.Children.Clear();
Image aImg = new Image();
aImg.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
aImg.VerticalAlignment = System.Windows.VerticalAlignment.Top;
aImg.Height = 70;
aImg.Width = 250;
aImg.Stretch = Stretch.Uniform;
aImg.Source = theImage;
ImageGrid.Children.Add(aImg);
}
文件系统监视程序myWatcher;
调度员myDisp;
私有整数计数=0;
公共主窗口()
{
初始化组件();
myWatcher=新的FileSystemWatcher();
路径=@“C:\Test”;
myWatcher.Filter=“hospital.png”;
myWatcher.NotifyFilter=NotifyFilters.LastWrite;
myWatcher.Changed+=myWatcher\u Changed;
myWatcher.EnableRaisingEvents=true;
myDisp=Dispatcher.CurrentDispatcher;
}
void myWatcher\u已更改(对象发送方、文件系统目标)
{
myWatcher.EnableRaisingEvents=false;
字符串aImgPath=@“C:\Test\hospital.png”;
if(File.Exists(aImgPath))
{
BitmapImage src=新的BitmapImage();
src.BeginInit();
src.UriSource=新Uri(aImgPath,UriKind.Relative);
src.CacheOption=BitmapCacheOption.OnLoad;
src.EndInit();
src.Freeze();
myDisp.Invoke(DispatcherPriority.Normal,新操作(UpdateImage),src);
}
myWatcher.EnableRaisingEvents=true;
}
无效更新图像(位图图像)
{
ImageGrid.Children.Clear();
图像aImg=新图像();
aImg.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
aImg.VerticalAlignment=System.Windows.VerticalAlignment.Top;
目标高度=70;
目标宽度=250;
目标拉伸=拉伸均匀;
aImg.Source=图像;
ImageGrid.Children.Add(aImg);
}
此代码在文件hospital.png更新时执行;但用户界面仍然显示第二次之后的旧图像。我有没有做错什么?问题是WPF缓存了图像文件Uri。因为它从不更改,所以不会加载新图像 但是,您可以从文件流加载映像:
if (File.Exists(aImgPath))
{
var src = new BitmapImage();
using (var fs = new FileStream(
aImgPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
src.BeginInit();
src.StreamSource = fs;
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
}
src.Freeze();
myDisp.Invoke(DispatcherPriority.Normal, new Action<BitmapImage>(UpdateImage), src);
}
if(File.Exists(aImgPath))
{
var src=新的位图图像();
使用(var fs=newfilestream)(
aImgPath,FileMode.Open,FileAccess.Read,FileShare.Read)
{
src.BeginInit();
src.StreamSource=fs;
src.CacheOption=BitmapCacheOption.OnLoad;
src.EndInit();
}
src.Freeze();
myDisp.Invoke(DispatcherPriority.Normal,新操作(UpdateImage),src);
}