apologies if this isn't worth asking a question about but I'm puzzled as to why what I'm doing isn't working.
I'm making a WP8 app and I want to display the current date and time (which will refresh each minute) on the screen. I've managed to get the date to work fine, but the time just won't show up for me and I've followed numerous tutorials. It's probably something really stupid but I can't figure it out. I'm also using MVVM pattern which is pretty new to me, so it could be something to do with that?
Here's my ViewModel class;
public class SleepTrackerViewModel : INotifyPropertyChanged
{
private string _currentTime, _currentDate;
public event PropertyChangedEventHandler PropertyChanged;
public SleepTrackerViewModel()
{
CurrentDateText();
DispatcherTimerSetup();
}
private void DispatcherTimerSetup()
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromMinutes(1);
dispatcherTimer.Tick += new EventHandler(CurrentTimeText);
dispatcherTimer.Start();
}
private void CurrentDateText()
{
CurrentDate = DateTime.Now.ToString("dddd dd MMMM yyyy");
}
private void CurrentTimeText(object sender, EventArgs e)
{
CurrentTime = DateTime.Now.ToString("HH:mm");
}
public string CurrentTime
{
get { return _currentTime; }
set
{
if (_currentTime != null)
_currentTime = value;
OnPropertyChanged("CurrentTime");
}
}
public string CurrentDate
{
get { return _currentDate; }
set
{
if (_currentDate != value)
_currentDate = value;
OnPropertyChanged("CurrentDate");
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
And in my XAML code I have;
<TextBlock FontSize="25" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Top" Text="{Binding CurrentTime}"/>
Also in the XAML code, I have my xmlns:local set to my ViewModel folder, and also have the UserControl.DataContext set to the correct class. As I've mentioned, the date shows up fine. The time did show up randomly in the designer view but when I ran the code it just disappeared.
Thanks.