I've never used IOC in any of my apps, and today I decided to learn how to use it but I'm having some issues with bindings.
What happens is that bound values are updated as soon as the page shows, but changing some of them later doesn't reflect to the page.
Here are some snippets of the part that's causing issues:
service
public class TimerService : BindableBase, ITimerService
{
public TimerService()
{
RemainingTime = UpdateInterval;
var updateTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1)
};
updateTimer.Tick += UpdateTimerOnTick;
updateTimer.Start();
}
private double _remainingTime;
public int UpdateInterval { get; set; } = 90;
public double RemainingTime
{
get { return _remainingTime; }
set {
// From Template10, it calls RaisePropertyChanged (and even calling it manually doesn't help)
Set(ref _remainingTime, value);
}
}
private void UpdateTimerOnTick(object sender, object o)
{
RemainingTime -= 1;
if (Math.Abs(RemainingTime) < 0.05) RemainingTime = UpdateInterval;
// Timer is running fine as I can see the prints
Debug.WriteLine($"{RemainingTime}");
}
}
locator
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<ITimerService, DesignTimeTimerService>();
}
else
{
SimpleIoc.Default.Register<ITimerService, TimerService>(true);
}
SimpleIoc.Default.Register<MainPageViewModel>();
}
public MainPageViewModel MainPageViewModel => ServiceLocator.Current.GetInstance<MainPageViewModel>();
}
viewModel
public class MainPageViewModel : ViewModelBase
{
public ITimerService TimerService { get; }
public MainPageViewModel(ITimerService timerService)
{
TimerService = timerService;
}
}
page
<Page x:Class="Views.MainPage"
...
DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=MainPageViewModel}"
mc:Ignorable="d">
...
<ProgressBar Maximum="{x:Bind ViewModel.TimerService.UpdateInterval}"
...
Value="{x:Bind ViewModel.TimerService.RemainingTime, Mode=OneWay, Converter={StaticResource EmptyConverter}}" />
...
(.cs file)
private MainPageViewModel ViewModel => DataContext as MainPageViewModel;
What I'd expect is to have ProgressBar
's value decreasing as the timer goes, but it just stays still on the very first value that I set.
I've also tried adding an empty converter to see if something happens but it's like ProgressBar
never receives the update event.
Do you have any hints on this?