I have a property defined in my viewmodel which is supposed to check if todays internet date is same as the system date and store it in the property as some string so that I can bind it to a textblock text or a label content.
But this check must run every 5 second as long as the app is open, that means the value of the property can change and it should reflect on the view accordingly.
How can I do this following mvvm pattern and using .Net 4.x?
I've done
Then in the label :
I've also added an IValueConverter to convert the color of the text depending of whether msg is equal to a certain text or not:
But it is not working as intended, initially the value changes like if I change the system date or disconnect internet the property updates but after that I make the system date correct or activate internet the property does not update.
Can someone help?
But this check must run every 5 second as long as the app is open, that means the value of the property can change and it should reflect on the view accordingly.
How can I do this following mvvm pattern and using .Net 4.x?
I've done
C#:
public class BaseVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class VM : BaseVM
{
private static System.Timers.Timer chkDate;
public string msg {get;set;}
public VM()
{
chkDate = new System.Timers.Timer();
chkDate.Interval = 5000; // every five seconds
chkDate.Elapsed += VerifyDate;
chkDate.AutoReset = true;
chkDate.Enabled = true;
}
public void VerifyDate(Object source, System.Timers.ElapsedEventArgs e)
{
if (IsInternetAvailable())
{
if (DateTime.Today.ToString("dd-MMM-yyyy") == GetInternetTime().ToString("dd-MMM-yyyy"))
{
msg="Machine date verified!";
}
else
msg="Machine date is not correct!";
}
else
{
msg="Machine date cannot be verified! Check internet connectivity !!!";
}
OnPropertyChanged("msg");
}
}
Then in the label :
<Label Content="{Binding msg}" Foreground="{Binding msg, Converter={StaticResource TextToColorConverter}}" ... />
I've also added an IValueConverter to convert the color of the text depending of whether msg is equal to a certain text or not:
C#:
public class TextToColorConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string str = value as string;
if (value == null || str=="Machine date verified!")
return new SolidColorBrush(Colors.LightGreen);
else
return new SolidColorBrush(Colors.Red);
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
But it is not working as intended, initially the value changes like if I change the system date or disconnect internet the property updates but after that I make the system date correct or activate internet the property does not update.
Can someone help?