Issue
DateTime.Now does not update the hour minutes and seconds in real time when the application is opened in android, what are my options?. also as it could capture the month, day, year, hour and minutes, you could show me your examples.
Solution
You can create a ViewModel and bind it with label or something else.
ViewModel is like:
class ClockViewModel : INotifyPropertyChanged
{
DateTime dateTime;
public event PropertyChangedEventHandler PropertyChanged;
public ClockViewModel()
{
this.DateTime = DateTime.Now;
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
this.DateTime = DateTime.Now;
return true;
});
}
public DateTime DateTime
{
set
{
if (dateTime != value)
{
dateTime = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DateTime"));
}
}
}
get
{
return dateTime;
}
}
}
And here is xaml:
<Label Text="{Binding DateTime, StringFormat='{0:T}'}"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center">
<Label.BindingContext>
<local:ClockViewModel />
</Label.BindingContext>
</Label>
</ContentPage>
Answered By - Adrain Zhu -MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.