I am trying to build a custom control, based on Image
, which simply makes the image reload (from the same URI) on a timer. The original image is displaying fine, but it doesn't seem to refresh.
And here is custom control:
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);
}
}
And I'm using it like so:
<custom:RefreshImage>
<Image.Source>
<BitmapImage UriCachePolicy="NoCacheNoStore"
CreateOptions="IgnoreImageCache" CacheOption="None"
UriSource="{Binding Uri}"/>
</Image.Source>
</custom:RefreshImage>
Documentation for InvalidateProperty seems to indicate it's exactly what I need:
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.
Raising an INotifiyPropertyChanged.PropertyChanged
event on the Uri
also does not trigger the image to reload.