diff options
Diffstat (limited to 'Xamarin.Forms.Xaml/MarkupExtensions/ArrayExtension.cs')
-rw-r--r-- | Xamarin.Forms.Xaml/MarkupExtensions/ArrayExtension.cs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Xamarin.Forms.Xaml/MarkupExtensions/ArrayExtension.cs b/Xamarin.Forms.Xaml/MarkupExtensions/ArrayExtension.cs new file mode 100644 index 00000000..9f594132 --- /dev/null +++ b/Xamarin.Forms.Xaml/MarkupExtensions/ArrayExtension.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Xamarin.Forms.Xaml +{ + [ContentProperty("Items")] + public class ArrayExtension : IMarkupExtension<Array> + { + public ArrayExtension() + { + Items = new List<object>(); + } + + public IList Items { get; } + + public Type Type { get; set; } + + public Array ProvideValue(IServiceProvider serviceProvider) + { + if (Type == null) + throw new InvalidOperationException("Type argument mandatory for x:Array extension"); + + if (Items == null) + return null; + + var array = Array.CreateInstance(Type, Items.Count); + for (var i = 0; i < Items.Count; i++) + ((IList)array)[i] = Items[i]; + + return array; + } + + object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) + { + return (this as IMarkupExtension<Array>).ProvideValue(serviceProvider); + } + } +}
\ No newline at end of file |