b290f44ab4612ddb2e30a1a29366a379a83fa3fd
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / MarkupExtensions / ArrayExtension.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using Tizen.NUI.Binding;
5
6 namespace Tizen.NUI.Xaml
7 {
8         [ContentProperty("Items")]
9         [AcceptEmptyServiceProvider]
10         internal class ArrayExtension : IMarkupExtension<Array>
11         {
12                 public ArrayExtension()
13                 {
14                         Items = new List<object>();
15                 }
16
17                 public IList Items { get; }
18
19                 public Type Type { get; set; }
20
21                 public Array ProvideValue(IServiceProvider serviceProvider)
22                 {
23                         if (Type == null)
24                                 throw new InvalidOperationException("Type argument mandatory for x:Array extension");
25
26                         if (Items == null)
27                                 return null;
28
29                         var array = Array.CreateInstance(Type, Items.Count);
30                         for (var i = 0; i < Items.Count; i++)
31                                 ((IList)array)[i] = Items[i];
32
33                         return array;
34                 }
35
36                 object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
37                 {
38                         return (this as IMarkupExtension<Array>).ProvideValue(serviceProvider);
39                 }
40         }
41 }