【发布时间】:2022-04-19 16:05:37
【问题描述】:
我正在尝试构建一个基于Image 的自定义控件,它只是在计时器上重新加载图像(从相同的URI)。原始图像显示正常,但似乎没有刷新。
这里是自定义控件:
public class RefreshImage : Image
{
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
this.Loaded += RefreshImage_Loaded;
}
private void RefreshImage_Loaded(object sender, RoutedEventArgs e)
{
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += RefreshImage_Tick;
timer.Start();
}
private void RefreshImage_Tick(object sender, System.EventArgs e)
{
var bm = (BitmapImage) Source;
bm.InvalidateProperty(BitmapImage.UriSourceProperty);
}
}
我就是这样使用它的:
<custom:RefreshImage>
<Image.Source>
<BitmapImage UriCachePolicy="NoCacheNoStore"
CreateOptions="IgnoreImageCache" CacheOption="None"
UriSource="{Binding Uri}"/>
</Image.Source>
</custom:RefreshImage>
InvalidateProperty 的文档似乎表明这正是我所需要的:
您还可以使用 InvalidateProperty 强制重新评估 绑定到无法实现的数据源 推荐 INotifyPropertyChanged 通知机制。
在Uri 上引发INotifiyPropertyChanged.PropertyChanged 事件也不会触发图像重新加载。
【问题讨论】:
-
当图像在您的应用程序中打开时,您可以更改(写入)图像吗?
-
出于好奇,我试图看看这是否仅适用于 UriSource 上的简单数据绑定......它没有。请参阅 2006 年的线程:social.msdn.microsoft.com/Forums/vstudio/en-US/… 似乎 UriSource 是一个 set once 依赖属性。随后的更改似乎无济于事
-
这个问题不是重复的。这个问题是关于为什么 UriSource 一旦设置就不能更改。
标签: wpf