Custom Events and Xamarin.Forms Effect

This post will walk you through setting up a Xamarin Platform Effect and show you how to wire up an event handler so you can leverage native APIs in the case that the Xamarin.Forms wrapper doesn’t yet have. For this example, I’ll use the Telerik UI for Xamarin RadCalendar.

tl;dr Full source code on GitHub here.

I get frequently asked how to enable RangeSelection (aka Multiple Selection) for the RadCalendar. The native platform calendar controls all have Range Selection available, so we only need to create a  Platform Effect to access that native feature.

Note: If you’re not familiar with Effects, I recommend visiting this article first to understand the fundamentals (it’s a quick tutorial).

The main things I need are:

  • DateTime StartDate
  • DateTime EndDate
  • DateRangeChangedEventArgs (class)
  • DateRangeChanged (event)
  • DateRangeChanged (delegate)

I’ll walk you through the different parts below, you can also see just the relevant classes in this GitHub Gist or get everything in this GitHub repo

Class Library

First, let’s get the event args out of the way because we’ll need this defined before writing the Effect.

(view code) Portable/Effects/DateRangeChangedEventArgs.cs


Now we can move on to the Effect definition that lives in the class library project.  The class defines the rest of items I listed above, I’ve called out how the event is invoked, thus subscribers to the vent will have their event handlers executed.

(view code) Portable/Effects/RangeSelectionEffect.Forms.cs


With this set up , we can add the Effect to the Xamarin.Forms XAML RadCalendar instance:

(view code) Portable/MainPage.xaml


Code behind, this is just to set the start and end dates to test the Effect:

(view code) Portable/MainPage.xaml.cs


But we can’t run it just yet. It’s time to implement the native Effect classes., it’s where the magic happens. I’ll go through each platform separately instead of hitting you over the head with it all at once.


The calendar control for UWP is the UI for UWP RadCalendar.  In the documentation, we can see it supports multiple selection by flipping the SelectionMode flag to Multiple.

The next thing to consider is how to actually set the date range. This is done using the SelectedDateRange property to an instance of CalendarDateRange. This is what we needed the event for! You’ll see that when the Effect is Attached to the control

(view code) UWP/Effects/RangeSelectionEffect.Uwp.cs



For iOS the native control is a UI for Xamarin.iOS TKCalendar and in a similar fashion as UWP, we find the Selection modes to support Range Selection.

Notice we need to use the native control’s selection property and convert from DateTime to NSDate

(view code) iOS/Effects/RangeSelectionEffect.iOS.cs



And finally, the same approach is used for the native Android calendar, UI for Xamarin.Android RadCalendarView.

Notice we need to use the native control’s selection property and convert from DateTime to Java Calendar

(view code) Android/Effects/RangeSelectionEffect.Android.cs


And that’s it! All three platform’s Calendar control now will show range selection.


This isn’t production-ready code, it’s proof of concept and there are no defensive techniques in place (i.e. try/catch). I wanted to keep it as simple as possible to focus on the concepts.

If you want to use the code for your app, I’m happy I could help (MIT license). Just please don’t copy-paste it all and call it a day, then ping me on Twitter later and say “it’s broken!”.  The native platform logic should be inside try catch blocks and I would make sure the DateTime conversions are accurate for your needs.





Epic Build 2018 Playlists

Your one-stop shop for Microsoft Build 2018 sessions, slide decks, post links, keynotes and much more.

Living Document Note:

These Playlists will continue to be reordered and updated in real time as new content becomes available. However, the links will always remain the same and you can refresh the playlist page to get any updated ordering/content.

Migrating from PCLStorage to .NET Standard 2.0

If you’re a Xamarin Forms developer, you’ve likely used PCLStorage (or other Dependency Service) to interact with the target platform’s file system in the portable class library’s code. However, since November 2017, Xamarin.Forms now uses a .NET Standard 2.0 class library and PCLStorage is no longer supported.


This isn’t a problem because in .NET Core 2.0 you now have access to System.IO.File’s GetFolderPath and SpecialFolder methods (see System.IO.File in .NET Core 2.0 docs).

Thus, you can get a path to the app’s local folder (the one that the app can save to) without having to write a Dependency Service (which is what PCLStorage does) by using:

var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);


IMPORTANT: Make sure you always use Path.Combine to create the file path because different platforms use different path separators

var filePath = Path.Combine(LocalFolder, "notes.txt");


With a reference to the file path,  you can now access the file. For example reading the text:

var notes = File.ReadAllText(filePath);

Functional Demo

As a very simple example, I created a couple extension methods for Stream and Byte[] in the below FileExtensions class.

To test it, I created a ContentPage that downloads an image, saves it using the extension method and set a FileImageSource to confirm that it’s a file (instead of just using a StreamImageSource).


Note that the extension methods are very basic and shouldn’t be used in production as-is (i.e. no defensive programming code).

Here is the result at runtime on UWP:


Custom Themes in UI for Xamarin

Telerik UI for Xamarin comes with two themes out of the box, Default and Blue. You can easily style individual items, but you can also define an entire theme at once in a separate ResourceDictionary.

With this approach you can swap out ResourceDictionaries at runtime for a nice user-selected theme feature in your app’s settings. Let me walk you through a very simple demo to illustrate the approach.

Step 1. Create your custom theme’s ResourceDictionary

Xamarin.Forms doesn’t have a good ResourceDictionary template, so start with a Content Page (XAML) template and then change the ContentPage type to “ResourceDictionary” in both the XAML and code-behind.

Now, you can add in all the styles and colors you want in that one dictionary. You can find the Color names we use for your controls in the Themes Overview documentation.

In the custom dark theme example below, the Color resource key values are pretty clear as to what they do.

Step 2. ThemeHelper Class

I usually like to create a static class like this so that I can change the theme from anywhere in the app, but isn’t necessary. You can put this logic entirely in your settings page if you’d prefer.

You may have noticed that I’m using a RadResourceDictionary. We created this when Xamarin.Forms didn’t have MergeWith support. You can use a Xamarin.Forms ResourceDictionary type if you’re using a newer version that supports it.

Step 3. Runtime

I’m using a RadSegmentedControl to change the theme, and a RadListView to easily see the difference between themes. You can of course use whatever UI control you’d like to change themes, for example a Picker.

The important thing is that you call the helper class’s ChangeTheme method and pass the theme name that you need to change to:

That’s it!

When selecting one of the options, you’ll get one of the three results you see in this article’s header image.

If you have any questions feel free to reach out to me on Twitter at @lancewmccarthy.