11 Answers, 1 is accepted
I did this...
void Handle_CellTapped(object sender, CalendarCell e)
{
if (e is CalendarDateCell dateCell)
{
await ShowDayAppointments(dateCell.Date);
}
}
Did Xamarin Studio generate the correct signature?
CellTapped event args do not provide the date of the cell, because there are many different kinds of cells, not all of them have DateTime objects associated (you can see the type by using the args's Type property).
A better event for your needs is the SelectionChanged event:
private
void
Calendar_OnSelectionChanged(
object
sender, ValueChangedEventArgs<
object
> e)
{
var lastSelectedDate = (DateTime)e.PreviousValue;
var currentlySelectedDate = (DateTime)e.NewValue;
}
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
In this case, you can just read the calendar's SelectedDate property. For example, you can make sure that the user tapped the cell type that you only want to get the date from, then use the value of SelectedDate:
private
void
Calendar_OnCellTapped(
object
sender, CalendarCell e)
{
if
(e.Type == CalendarCellType.Date)
{
var date = calendar.SelectedDate;
}
}
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
But if the tapped cell is a new cell, the SelectedDate is out of date because its value is updated after OnCellTapped.
I mentioned this in my original post.
Correct, this is why we don't recommend using CellTapped for data requirements, it's designed use is not for determining the date/time of the cell (it's used for many types of cells, not just cell associated with a DateTime).
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
I've created an experimental approach that you can try that will give you the tapped cell's date (regardless how how many times it is tapped). This uses the cell's text to create a DateTime instance.
private void Calendar_OnCellTapped(object sender, CalendarCell e)
{
if (e.Type == CalendarCellType.Date)
{
var computedDate = new DateTime(
calendar.DisplayDate.Year,
calendar.DisplayDate.Month,
Convert.ToInt32(e.Text));
Debug.WriteLine($"CellTapped - EstimatedDate of Tapped Cell: {computedDate}");
}
}
Here's my output after testing with two cells in Month view:
CellTapped - EstimatedDate of Tapped Cell: 4/10/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/10/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/10/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/11/2018 12:00:00 AM
CellTapped - EstimatedDate of Tapped Cell: 4/11/2018 12:00:00 AM
I urge that you test this thoroughly before using it in an application, this is not the designed used of the event and we don't recommend it for production. It works in Month view as-is, but I would add a bit of defensive programming for when the text can't be converted to Int, if the combination doesn't convert to a valid DateTime, etc.
I hope this helps.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
I agree that the original approach is the best option. If you have any further issues, please share the problematic code and I'll be happy to assist.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Hi Matthew,
This is expected, the CellTapped event is not intended for date value selection. Unless you have a special custom condition, we strongly recommend using the built-in selection mechanism. See https://docs.telerik.com/devtools/xamarin/controls/calendar/calendar-selection
If you want to bypass the selection mechanism, this is not something we support, but is achievable with enough custom logic.
For example, using your scenario, you need a few layers of checks for all the properties of a cell. If it is a disabled cell, then try reading the text in that cell to see if it's between 1 and 7 or between 23 and 31. If you know that, then you can make a logical branch that adds or subtracts a month from the current date's month
As you can see this is a lot of custom logic to work around what we already have built-in. Ian has a special case that made it worth doing this.
Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik