MonoMobile.Views.ViewParser.GetCommandForMember C# (CSharp) Method

GetCommandForMember() public static method

public static GetCommandForMember ( object view, MemberInfo member ) : ReflectiveCommand
view object
member System.Reflection.MemberInfo
return ReflectiveCommand
	    public static ReflectiveCommand GetCommandForMember(object view, MemberInfo member)
		{
			string propertyName = string.Empty;
			PropertyInfo propertyInfo = null;
			var commandOption = CommandOption.Disable;

			var buttonAttribute = member.GetCustomAttribute<ButtonAttribute>();
			if (buttonAttribute != null)
			{
				propertyName = buttonAttribute.CanExecutePropertyName;
				commandOption = buttonAttribute.CommandOption;
			}

			var toolbarButtonAttribute = member.GetCustomAttribute<ToolbarButtonAttribute>();
			if (toolbarButtonAttribute != null)
			{
				propertyName = toolbarButtonAttribute.CanExecutePropertyName;
				commandOption = toolbarButtonAttribute.CommandOption;
			}

			var navbarButtonAttribute = member.GetCustomAttribute<NavbarButtonAttribute>();
			if (navbarButtonAttribute != null)
			{
				propertyName = navbarButtonAttribute.CanExecutePropertyName;
				commandOption = navbarButtonAttribute.CommandOption;
			}

			var methodInfo = member as MethodInfo;

			if (methodInfo == null)
				throw new Exception(string.Format("Method not found {0}", member.Name));
			
			object source = view;
			if (!string.IsNullOrEmpty(propertyName))
			{
				PropertyInfo property = source.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
				
				if (property == null)
				{
					var dataContext = view as IDataContext<object>;
					if (dataContext != null)
					{
						var vm = dataContext.DataContext;
						if (vm != null)
						{
							source = vm;
							property = source.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
						}
					}
				}

				if (property != null)
				{
					if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?))
					{
						propertyInfo = property;
					}
					else
					{
						throw new Exception(string.Format("Property {0} cannot be used for CanExecute property because it does not have a return type of bool", property.Name));
					}
				}
			}

			return new ReflectiveCommand(view, methodInfo, source, propertyInfo) { CommandOption = commandOption };
		}

Usage Example

Beispiel #1
0
        protected virtual void Prepare()
        {
            CommandMap.Clear();

            _ActualActionSheet            = new UIActionSheet(Title);
            _ActualActionSheet.Dismissed += HandleDismissed;

            var methods = GetType().GetMethods().Where((methodInfo) => methodInfo.GetCustomAttribute <ButtonAttribute>(true) != null).ToList();

            ICommand command       = null;
            var      destroyMethod = methods.Where((methodInfo) => methodInfo.Name == "Destroy").SingleOrDefault();

            if (destroyMethod != null)
            {
                command = ViewParser.GetCommandForMember(this, destroyMethod);
                var destroyIndex = AddButtonToSheet(GetTitle(destroyMethod), command);
                if (destroyIndex > -1)
                {
                    _ActualActionSheet.DestructiveButtonIndex = destroyIndex;
                }
            }

            foreach (var method in methods)
            {
                if (method.Name != "Cancel" && method.Name != "Destroy")
                {
                    command = ViewParser.GetCommandForMember(this, method);
                    AddButtonToSheet(GetTitle(method), command);
                }
            }

            var cancelMethod = methods.Where((methodInfo) => methodInfo.Name == "Cancel").SingleOrDefault();

            if (cancelMethod != null)
            {
                command = ViewParser.GetCommandForMember(this, cancelMethod);
                var cancelIndex = AddButtonToSheet(GetTitle(cancelMethod), command);
                if (cancelIndex > -1)
                {
                    _ActualActionSheet.CancelButtonIndex = cancelIndex;
                }
            }
        }
All Usage Examples Of MonoMobile.Views.ViewParser::GetCommandForMember