gpt4 book ai didi

wpf - 强制 WPF BitmapImage 从其 UriSource 重新加载

转载 作者:行者123 更新时间:2023-12-05 07:45:05 24 4
gpt4 key购买 nike

我正在尝试构建一个基于 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 的文档似乎表明这正是我所需要的:

You can also use InvalidateProperty to force re-evaluation of a binding against a data source that is not able to implement the recommended INotifyPropertyChanged notification mechanism.

Uri 上引发 INotifiyPropertyChanged.PropertyChanged 事件也不会触发图像重新加载。

最佳答案

一般来说,BitmapSource 似乎是不可变对象(immutable对象)。一旦它们完成初始化,任何进一步的属性更改都将被忽略。我怀疑这包括 UriSource

以下阶段在 MSDN post on creating custom ImageSources 中经常重复:

The ISupportInitialize interface is used to snap the values of theproperties when initialization is complete. Further changes to theproperties are ignored.

关于wpf - 强制 WPF BitmapImage 从其 UriSource 重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com