我想建立一個自定義控件,基於Image
,它只是在一個計時器上(從同一個URI)重新加載圖像。原始圖像顯示正常,但似乎沒有刷新。強制WPF BitmapImage從其UriSource重新加載
這裏是自定義控件:
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強制對數據源臨時綁定 綁定進行重新評估t無法實現推薦的INotifyPropertyChanged通知機制 。
提高INotifiyPropertyChanged.PropertyChanged
事件的Uri
也不會觸發圖像重新加載。
在應用程序中打開圖像時,可以更改(寫入)圖像嗎? – Ron
出於好奇,我試圖看看這是否只適用於UriSource上的簡單數據綁定......它不是。從2006年開始查看線程:https://social.msdn.microsoft.com/Forums/vstudio/en-US/85cffb5c-05a4-4653-a263-183f20ddd2c2/xaml-setting-bitmapimageurisource-from-binding-doesnt-work? forum = wpf似乎UriSource是一次依賴項屬性。隨後的變化似乎什麼都不做 – Schneider
這個問題不是重複的。這個問題是關於爲什麼UriSource一旦設置就無法更改。 – Schneider