Beispiel #1
0
        private Element GetElementForMember(object dataContext, MemberInfo member)
        {
            string  caption  = null;
            Element element  = null;
            var     bindings = new List <Binding>();

            var captionAttribute        = member.GetCustomAttribute <CaptionAttribute>();
            var orderAttribute          = member.GetCustomAttribute <OrderAttribute>();
            var bindAttributes          = member.GetCustomAttributes(typeof(BindAttribute), false);
            var popOnSelectionAttribute = member.GetCustomAttribute <PopOnSelectionAttribute>();

            //	var memberDataContext = GetDataContextForMember(dataContext, ref member);
            var memberDataContext = dataContext;

            if (captionAttribute != null)
            {
                caption = captionAttribute.Caption;
            }
            else
            {
                caption = MakeCaption(member.Name);
            }

            Type memberType = GetTypeForMember(member);

            if (!(member is MethodInfo))
            {
                foreach (BindAttribute bindAttribute in bindAttributes)
                {
                    bindings.Add(bindAttribute.Binding);
                }

                var valueBinding = bindings.Where((b) => b.TargetPath == "Value").FirstOrDefault() != null;

                if (!valueBinding)
                {
                    bindings.Add(new Binding(member.Name, null));
                }

                foreach (var binding in bindings)
                {
                    if (string.IsNullOrEmpty(binding.SourcePath))
                    {
                        binding.SourcePath = member.Name;
                    }
                    //			else
                    {
                        var sourceDataContext = memberDataContext;
                        var sourceProperty    = sourceDataContext.GetType().GetNestedProperty(ref sourceDataContext, binding.SourcePath, true);
                        if (sourceProperty == null)
                        {
                            sourceDataContext = dataContext;
                            sourceProperty    = sourceDataContext.GetType().GetNestedProperty(ref sourceDataContext, binding.SourcePath, true);
                        }

                        binding.Source = sourceDataContext;
                    }
                }
            }

            if (_ElementPropertyMap.ContainsKey(memberType))
            {
                element = _ElementPropertyMap[memberType](member, caption, dataContext, bindings);
            }
            else if (memberType.IsEnum)
            {
                SetDefaultConverter(member, "Value", new EnumConverter()
                {
                    PropertyType = memberType
                }, bindings);

                var csection     = new Section();
                var currentValue = GetValue(member, memberDataContext);
                int index        = 0;
                int selected     = 0;

                var pop = popOnSelectionAttribute != null;

                foreach (Enum value in Enum.GetValues(memberType))
                {
                    if (currentValue == value)
                    {
                        selected = index;
                    }
                    csection.Add(new RadioElement(value.GetDescription(), pop)
                    {
                        Index = index, Value = false
                    });
                    index++;
                }

                element = new RootElement <int>(caption, new RadioGroup(memberType.FullName, selected))
                {
                    csection
                };
                element.Caption        = caption;
                ((IRoot)element).Group = new RadioGroup(memberType.FullName, selected)
                {
                    EnumType = memberType
                };
                ((IRoot)element).CellStyle = UITableViewCellStyle.Value1;
            }
            else if (typeof(EnumCollection).IsAssignableFrom(memberType))
            {
                SetDefaultConverter(member, "Value", new EnumItemsConverter(), bindings);

                var csection = new Section()
                {
                    IsMultiselect = true
                };
                var collection = GetValue(member, memberDataContext);
                if (collection == null)
                {
                    var    collectionType = typeof(EnumCollection <>);
                    var    enumType       = memberType.GetGenericArguments()[0];
                    Type[] generic        = { enumType };

                    collection = Activator.CreateInstance(collectionType.MakeGenericType(generic));
                    (member as PropertyInfo).SetValue(memberDataContext, collection, new object[] {});
                }

                var index = 0;
                var items = (EnumCollection)collection;
                foreach (var item in items.AllValues)
                {
                    csection.Add(new CheckboxElement(item.Description, item.IsChecked, item.GroupName)
                    {
                        Index = index
                    });
                    index++;
                }

                element = new RootElement <int>(caption)
                {
                    csection
                };
                ((IRoot)element).CellStyle = GetCellStyle(member, UITableViewCellStyle.Value1);
            }
            else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(memberType))
            {
                SetDefaultConverter(member, "Value", new EnumerableConverter(), bindings);
                ListBoxElement listBox = new ListBoxElement();
                int            index   = 0;

                Type viewType = memberType.GetGenericArguments()[0];

                var rootAttribute = member.GetCustomAttribute <RootAttribute>();

                var items = (IEnumerable)GetValue(member, memberDataContext);
                foreach (var e in items)
                {
                    Element newElement = null;

                    newElement = CreateGenericRoot(viewType, null, e);
                    if (rootAttribute != null)
                    {
                        ((IRoot)newElement).ElementType = rootAttribute.DataTemplateType;
                    }

                    ((IRoot)newElement).ToolbarButtons = CheckForToolbarItems(e);
                    ((IRoot)newElement).NavbarButtons  = CheckForNavbarItems(e);

                    listBox.Add(newElement);
                    index++;
                }

                element                    = CreateGenericRoot(memberType, listBox, null);
                element.Caption            = caption;
                ((IRoot)element).CellStyle = GetCellStyle(member, UITableViewCellStyle.Default);
            }
            else
            {
                var nested = GetValue(member, memberDataContext);
                if (nested != null)
                {
                    var newRoot = CreateGenericRoot(memberType, null, nested);
                    newRoot.Caption            = caption;
                    ((IRoot)newRoot).CellStyle = GetCellStyle(member, UITableViewCellStyle.Default);

                    ((IRoot)newRoot).ToolbarButtons = CheckForToolbarItems(nested);
                    ((IRoot)newRoot).NavbarButtons  = CheckForNavbarItems(nested);

                    element = newRoot;
                }
            }

            if (orderAttribute != null && element != null)
            {
                element.Order = orderAttribute.Order;
            }

            var bindable = element as IBindable;

            if (bindable != null && bindings.Count != 0)
            {
                foreach (Binding binding in bindings)
                {
                    if (binding.TargetPath == null)
                    {
                        binding.TargetPath = "Value";
                    }

                    BindingOperations.SetBinding(bindable, binding.TargetPath, binding);
                }
            }

            return(element);
        }
Beispiel #2
0
		private Element GetElementForMember(object dataContext, MemberInfo member)
		{
			string caption = null;
			Element element = null;
			var bindings = new List<Binding>();

			var captionAttribute = member.GetCustomAttribute<CaptionAttribute>();
			var orderAttribute = member.GetCustomAttribute<OrderAttribute>();
			var bindAttributes = member.GetCustomAttributes(typeof(BindAttribute), false);
			var popOnSelectionAttribute = member.GetCustomAttribute<PopOnSelectionAttribute>();

		//	var memberDataContext = GetDataContextForMember(dataContext, ref member);
			var memberDataContext = dataContext;

			if(captionAttribute != null)
				caption = captionAttribute.Caption;
			else
				caption = MakeCaption(member.Name);

			Type memberType = GetTypeForMember(member);

			if (!(member is MethodInfo))
			{
				foreach (BindAttribute bindAttribute in bindAttributes)
				{
					bindings.Add(bindAttribute.Binding);
				}
				
				var valueBinding = bindings.Where((b)=>b.TargetPath == "Value").FirstOrDefault() != null;

				if (!valueBinding)
				{
					bindings.Add(new Binding(member.Name, null));
				}

				foreach(var binding in bindings)
				{
					if (string.IsNullOrEmpty(binding.SourcePath))
					{
						binding.SourcePath = member.Name;
					}
		//			else
					{
						var sourceDataContext = memberDataContext;
						var sourceProperty = sourceDataContext.GetType().GetNestedProperty(ref sourceDataContext, binding.SourcePath, true);
						if (sourceProperty == null)
						{
							sourceDataContext = dataContext;
							sourceProperty = sourceDataContext.GetType().GetNestedProperty(ref sourceDataContext, binding.SourcePath, true);
						}

						binding.Source = sourceDataContext;
					}
				}
			}

			if(_ElementPropertyMap.ContainsKey(memberType))
				element = _ElementPropertyMap[memberType](member, caption, dataContext, bindings);
			else if (memberType.IsEnum)
			{
				SetDefaultConverter(member, "Value", new EnumConverter() { PropertyType = memberType }, bindings);
				
				var csection = new Section();
				var currentValue = GetValue(member, memberDataContext);
				int index = 0;
				int selected = 0;

				var pop = popOnSelectionAttribute != null;

				foreach(Enum value in Enum.GetValues(memberType))
				{
					if (currentValue == value)
						selected = index;
					csection.Add(new RadioElement(value.GetDescription(), pop) { Index = index, Value = false});
					index++;
				}

				element = new RootElement<int>(caption, new RadioGroup(memberType.FullName, selected)) { csection };
				element.Caption = caption;
				((IRoot)element).Group = new RadioGroup(memberType.FullName, selected) { EnumType = memberType };
				((IRoot)element).CellStyle = UITableViewCellStyle.Value1;
			}
			else if (typeof(EnumCollection).IsAssignableFrom(memberType))
			{
				SetDefaultConverter(member, "Value", new EnumItemsConverter(), bindings);

				var csection = new Section() { IsMultiselect = true };
				var collection = GetValue(member, memberDataContext);
				if (collection == null)
				{
					var collectionType = typeof(EnumCollection<>);
					var enumType = memberType.GetGenericArguments()[0];
					Type[] generic = { enumType };

					collection = Activator.CreateInstance(collectionType.MakeGenericType(generic));
					(member as PropertyInfo).SetValue(memberDataContext, collection, new object[] {});
				}

				var index = 0;
				var items = (EnumCollection)collection;
				foreach (var item in items.AllValues)
				{
					csection.Add(new CheckboxElement(item.Description, item.IsChecked, item.GroupName) { Index = index });
					index++;
				}
				
				element = new RootElement<int>(caption) { csection };
				((IRoot)element).CellStyle = GetCellStyle(member, UITableViewCellStyle.Value1);
			}
			else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(memberType))
			{
				SetDefaultConverter(member, "Value", new EnumerableConverter(), bindings);
				ListBoxElement listBox = new ListBoxElement();
				int index = 0;

				Type viewType = memberType.GetGenericArguments()[0];
				
				var rootAttribute = member.GetCustomAttribute<RootAttribute>();

				var items = (IEnumerable)GetValue(member, memberDataContext);
				foreach (var e in items)
				{
					Element newElement = null;

					newElement = CreateGenericRoot(viewType, null, e);
					if (rootAttribute != null)
						((IRoot)newElement).ElementType = rootAttribute.DataTemplateType; 
			
					((IRoot)newElement).ToolbarButtons = CheckForToolbarItems(e);
					((IRoot)newElement).NavbarButtons = CheckForNavbarItems(e);

					listBox.Add(newElement);
					index++;
				}

				element = CreateGenericRoot(memberType, listBox, null);
				element.Caption = caption;
				((IRoot)element).CellStyle = GetCellStyle(member, UITableViewCellStyle.Default);
			}
			else
			{
				var nested = GetValue(member, memberDataContext);
				if (nested != null)
				{
					var newRoot = CreateGenericRoot(memberType, null, nested);
					newRoot.Caption = caption;
					((IRoot)newRoot).CellStyle = GetCellStyle(member, UITableViewCellStyle.Default);

					((IRoot)newRoot).ToolbarButtons = CheckForToolbarItems(nested);
					((IRoot)newRoot).NavbarButtons = CheckForNavbarItems(nested);

					element = newRoot;
				}
			}
			
			if (orderAttribute != null && element != null)
				element.Order = orderAttribute.Order;

			var bindable = element as IBindable;
			if (bindable != null && bindings.Count != 0)
			{
				foreach (Binding binding in bindings)
				{
					if (binding.TargetPath == null)
					{
						binding.TargetPath = "Value";
					}

					BindingOperations.SetBinding(bindable, binding.TargetPath, binding);
				}
			}

			return element;
		}