Hello every Teleriker :)
Does anyone managed to set the ViewMode on a RadCalendar on Android with a custom renderer that get the good ViewMode value ?
I set the ViewMode when the RadCalendar NativeControlLoaded event is triggered.
((RadCalendar)sender).TrySetViewMode(CalendarViewMode.Week);
In the code behind of my page, the ViewMode seems to bet set.
But in the CalendarCustomRenderer, e.NewElement.ViewMode always return Month (I'm expecting a Week value).
I would like to add it's working great on iOS (I can cast the Control.Presenter to a TKCalendarWeekPresenter without error).
I've created this custom render because I want to hide the title of the calendar in ViewMode Week.
(again, it's working as expected on iOS).
Is it a known issue (if it is so, should I create a ticket for that ?) or am I missing something ?
I have attached a screenshot of my breackpoint with the wrong value.
I wish a good day to everyone who red this post.
6 Answers, 1 is accepted
Oups, I typed it wrong : right after the
((RadCalendar)sender).TrySetViewMode(CalendarViewMode.Week);
((RadCalendar)sender).ViewMode still returns Month.
Funny, it seems to work if I do that :
protected override void OnElementChanged(ElementChangedEventArgs<
RadCalendar
> e)
{
base.OnElementChanged(e);
if (Control != null)
{
if (e.OldElement != null)
e.OldElement.ViewChanged -= NewElement_ViewChanged;
e.NewElement.ViewChanged += NewElement_ViewChanged;
}
}
private void NewElement_ViewChanged(object sender, ValueChangedEventArgs<
CalendarViewMode
> e)
{
if (e.NewValue == CalendarViewMode.Day)
{
Control.ShowTitle = false;
}
else if (e.NewValue == CalendarViewMode.MultiDay)
{
Control.ShowTitle = false;
Control.ShowDayNames = false;
}
else if (e.NewValue == CalendarViewMode.Week)
{
Control.ShowTitle = false;
// Allow the control to set size to Auto in week view mode.
Control.SetStretchInWeekMode(false);
Control.InOriginalSizeForAllModes = false;
}
else if (e.NewValue == CalendarViewMode.Month)
{
Control.ShowTitle = true;
}
}
Not the implementation I wanted but if it works don't touch it :)
The reason you're seeing this behavior on Android is because the native Calendar instance doesn't change when calling SetViewMode (i.e. changing view modes). The renderer's OnElementChanged only fires when the native calendar instance get replaced, so you'll always get the default value for ViewMode.
You have taken the recommended approach by subscribing to the native calendar's ViewChanged event. That event will be invoked later, when the ViewMode is changed in Xamarin.Forms, and you can get the current value for ViewMode.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Hello Lance, thank you.
May I suggest you to ask your team to put those recommandation into the documentation ? It would save a lot of people's time to get this information (about the behavior on Android).
While you are here, can I ask you how to lock the ability of the RadCalendar to pinch to zoom in/out ?
In ViewMode Week, I don't find in the documentation how to prevent the user to zoom out ?
I need to remove this functionnality because I want the control to stay in ViewMode.Week. Right now, if I pinch the RadCalendar in ViewMode.Week, I got the ViewMode.Month and I don't want it as my RadControl has a height set to 100 and that's too small to display the ViewMode.Month.
Is it possible to achieve that ? If it is, can you show me the path to do so ? Is it possible to update the documentation too ? Again, it might help a lot of people as it should be a common requirement.
Thanks a lot Lance, have a good day.
The documentation has sections on the native control wrappers for folks who need to access the APIs of the native controls, but it's under the "Native Controls" part. Here's what that looks like:
I agree that might not be obvious when you're looking in the Xamarin.Forms documentation, or it might be off screen if you're nested a few levels in. This is why I added a highlighted note with a link to the native control's documentation the top of each Calendar custom renderer article. As an example, see the RadCalendar Android Custom Renderer article.
Other Custom Renderer Articles
Your suggestion made me review some of the other custom renderer documentation to ensure there was a native link in those as well, and found that the Register Chart Renderer documentation needed some.So I added them in this pull request.
Thank you for mentioning this, as a small token of appreciation I have updated your Telerik Points.
KB Article
Generally speaking, it is expected that the native control is not re-instantiated when you change its properties, therefore the OnElementChanged event will not fire.
To help with this specific scenario of Detecting Initial View Changed on app launch in Custom Renderer, I'll write up a KB article (for our upcoming Knowledge Base) with the code example and explanation of life-cycle and differences between Android and iOS in this case.
Disabling Gestures
You can change the behavior of the Calendar's gesture by using the native control's Gesture Manager. See the Handled Gestures section of the native control's RadCalendarView: Gestures and Transitions article.
In C#, it is a boolean value set to one of the following properties:
For example:
rcv.GestureManager.SetPinchOpenToChangeDisplayMode(
false
);
rcv.GestureManager.SetPinchCloseToChangeDisplayMode(
false
);
If you have any further questions or trouble with the gestures, open a ticket with your code and I'll escalate it to the Android development team.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Hey Lance, thanks a lot !
And woaw, as you said I was too deep in the tree of XF documentation and did not see the Native Controls Wrappers section.
Thank you ! The documentation is nice !
You lead me on the path : what I was looking for is :
Control.GestureManager.SetSwipeUpToChangeDisplayMode(false);
Control.GestureManager.SetSwipeDownToChangeDisplayMode(false);
It works very well : now, users can't change the week view mode. Perfect :)
I will open a ticket for a hack I need : on Android, if your RadCalendar (ViewMode.Week) is embedded in a page inside a TabbedPage, if you want to scroll left/right (swipe), you have to "draw" a diagonal otherwise the swipe will be handled by the TabbedPage and will change the displayed page instead of changing the displayed date of the RadCalendar (Week).
I don't know if it is easy to address because I would like to keep this behavior if the user is swiping anywhere else than the RadCalendar (I have a RadCalendar and a ListView : if I swipe left or right on the ListView, right now, it's changing the displayed/selected page which is the behavior I need. When users slide right or left on the RadCelendar, I want to change the displayed week not the displayed page).
But I think a ticket might be better for that. The only drawback I see is the answer won't be public to anyone (contrary to the forum)
I've tagged your last answer as the answer of my problem.
I wish you a nice end of week. Regards