I am trying to run the clock for lets say 5 cities, like each city in each cell of the listview and clock(local time) running besides city name. (Screenshot attached below)
I am getting the local time of the city using offset value of that city.
And for the clock to move I used StartTimer, so that the time will change for every second.
Currently I am getting the offsetValue as zero there and so getting the UTC time there for all the cities as same.
I want all the cities with the local time displaying beside city name and the clock must move for every second.
Any help will be appreciated .. Thanks in advance.
Sample screenshot of cities with respective clocks
List<OffsetItems> items = new List<OffsetItems>();
items.Add(new OffsetItems() { CityName = "Hyderabad", Offset = 5.5 });
items.Add(new OffsetItems() { CityName = "London", Offset = 1 });
items.Add(new OffsetItems() { CityName = "Tokyo", Offset = 9 });
items.Add(new OffsetItems() { CityName = "New York", Offset = -5 });
items.Add(new OffsetItems() { CityName = "Dubai", Offset = 4 });
ListView lv = new ListView
{
SeparatorVisibility = SeparatorVisibility.None,
ItemsSource = items,
ItemTemplate = new DataTemplate(() =>
{
Label cityL = new Label()
{
TextColor = Color.Black,
HorizontalTextAlignment = TextAlignment.Start,
FontSize = Device.GetNamedSize(NamedSize.Small, new Label())
};
Label timeL = new Label()
{
TextColor = Color.Black,
HorizontalTextAlignment = TextAlignment.Center,
FontSize = Device.GetNamedSize(NamedSize.Small, new Label())
};
cityL.SetBinding<OffsetItems>(Label.TextProperty, indexer => indexer.CityName);
Label ll = new Label();
Device.StartTimer(TimeSpan.FromSeconds(1), () => {
ll.SetBinding<OffsetItems>(Label.TextProperty, indexer => indexer.Offset);
double offsetValue = Convert.ToDouble(ll.Text);
timeL.Text = DateTime.UtcNow.AddHours(offsetValue).ToString("hh:mm:ss tt, ddd dd-MMM-yyyy");
return true;
});
return new ViewCell
{
View = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children =
{
cityL,
timeL
}
}
};
})
};
Content = new StackLayout
{
Children = {
lv
}
};