Search code examples
xamlgenericsxamarin.forms

Xamarin.Forms using generic types as a type argument in XAML


I have a custom control:

public class CustomControl<T> : ContentView
{

}

I can create an instance of this custom control with the type argument TimeSpan in XAML in Xamarin.Forms with the following:

<controls:CustomControl x:TypeArguments="x:TimeSpan" />

How can I assign the type argument as Nullable<TimeSpan> so that I have an instance of CustomControl<Nullable<TimeSpan>>? I have tried the following:

<controls:CustomControl x:TypeArguments="x:Nullable<TimeSpan>" />

Solution

  • You can do it in codebehind with code like:

    CustomControl<TimeSpan?> customscontrol=new CustomControl<TimeSpan?>();