Binding не обновляет Image. MVVM, C#
Пытаюсь реализовать паттерн MVVM в UWP. Имеется страница, на которой картинки должны сменять друг друга через определённый промежуток времени. XAMl:
<vm:BannersPageViewModel />
</Page.DataContext>
<StackPanel>
<Image x:Name="Banners" Source="{Binding ImageSource}" />
</StackPanel>
Code Behind:
public MainPage()
{
InitializeComponent();
BannersPageViewModel bannersPageViewModel = new BannersPageViewModel();
bannersPageViewModel.SwitchingBanners();
}
ViewModel:
DispatcherTimer timer; //таймер
int counter = 0;
private List<string> _listImageSource = new List<string> { "/Assets/ImagesForBanners/Banner1.jpg",
"/Assets/ImagesForBanners/Banner2.jpg", "/Assets/ImagesForBanners/Banner3.jpg"
}; //картинки
private string _imageSource;
private string _videoSource;
private TimeSpan _bannerDisplayTime = new TimeSpan(0,0,1);
public string VideoSource { get => _videoSource; set => Set(ref _videoSource, value); }
public string ImageSource { get => _imageSource; set => Set(ref _imageSource, value); }
public TimeSpan BannerDisplayTime { get => _bannerDisplayTime; set => Set(ref _bannerDisplayTime, value); }
public void SwitchingBanners()
{
timer = new DispatcherTimer() { Interval = _bannerDisplayTime };
timer.Tick += PlayTime;
timer.Start();
}
public void PlayTime(object sender, object e)
{
counter++;
if (counter >= _listImageSource.Count) counter = 0;
ImageSource = _listImageSource[counter];
}
Базовая ViewModel:
public abstract class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string PropertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
protected virtual bool Set<T>(ref T field, T value, [CallerMemberName] string PropertyName = null)
{
if (Equals(field, value)) return false;
field = value;
OnPropertyChanged(PropertyName);
return true;
}
}
Если поставить точки остановы, то видно, что в _image поочерёдно присваиваются пути к картинкам, но view не обновляется. При этом если написать так:
_image = "/Assets/ImagesForBanners/Banner1.jpg"
то картинка в окне отобразится. Так же, если в code behind переместить код из viewModel, то всё работает. В чём может быть проблема?