示例#1
0
        internal static BindableProperty CreateAttached <TDeclarer, TPropertyType>(Expression <Func <BindableObject, TPropertyType> > staticgetter, TPropertyType defaultValue, BindingMode defaultBindingMode,
                                                                                   ValidateValueDelegate <TPropertyType> validateValue, BindingPropertyChangedDelegate <TPropertyType> propertyChanged, BindingPropertyChangingDelegate <TPropertyType> propertyChanging,
                                                                                   CoerceValueDelegate <TPropertyType> coerceValue, BindablePropertyBindingChanging bindingChanging, bool isReadOnly = false,
                                                                                   CreateDefaultValueDelegate <BindableObject, TPropertyType> defaultValueCreator = null)
        {
            if (staticgetter == null)
            {
                throw new ArgumentNullException(nameof(staticgetter));
            }

            Expression expr = staticgetter.Body;

            var unary = expr as UnaryExpression;

            if (unary != null)
            {
                expr = unary.Operand;
            }

            var methodcall = expr as MethodCallExpression;

            if (methodcall == null)
            {
                throw new ArgumentException("staticgetter must be a MethodCallExpression", nameof(staticgetter));
            }

            MethodInfo method = methodcall.Method;

            if (!method.Name.StartsWith("Get", StringComparison.Ordinal))
            {
                throw new ArgumentException("staticgetter name must start with Get", nameof(staticgetter));
            }

            string propertyname = method.Name.Substring(3);

            ValidateValueDelegate           untypedValidateValue           = null;
            BindingPropertyChangedDelegate  untypedBindingPropertyChanged  = null;
            BindingPropertyChangingDelegate untypedBindingPropertyChanging = null;
            CoerceValueDelegate             untypedCoerceValue             = null;
            CreateDefaultValueDelegate      untypedDefaultValueCreator     = null;

            if (validateValue != null)
            {
                untypedValidateValue = (bindable, value) => validateValue(bindable, (TPropertyType)value);
            }
            if (propertyChanged != null)
            {
                untypedBindingPropertyChanged = (bindable, oldValue, newValue) => propertyChanged(bindable, (TPropertyType)oldValue, (TPropertyType)newValue);
            }
            if (propertyChanging != null)
            {
                untypedBindingPropertyChanging = (bindable, oldValue, newValue) => propertyChanging(bindable, (TPropertyType)oldValue, (TPropertyType)newValue);
            }
            if (coerceValue != null)
            {
                untypedCoerceValue = (bindable, value) => coerceValue(bindable, (TPropertyType)value);
            }
            if (defaultValueCreator != null)
            {
                untypedDefaultValueCreator = o => defaultValueCreator(o);
            }

            return(new BindableProperty(propertyname, method.ReturnType, typeof(TDeclarer), defaultValue, defaultBindingMode, untypedValidateValue, untypedBindingPropertyChanged, untypedBindingPropertyChanging,
                                        untypedCoerceValue, bindingChanging, isReadOnly, untypedDefaultValueCreator));
        }
示例#2
0
 /// <include file="../../docs/Microsoft.Maui.Controls/BindableProperty.xml" path="//Member[@MemberName='Create']/Docs" />
 public static BindableProperty Create(string propertyName, Type returnType, [DynamicallyAccessedMembers(DeclaringTypeMembers)] Type declaringType, object defaultValue = null, BindingMode defaultBindingMode = BindingMode.OneWay,
                                       ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
                                       CoerceValueDelegate coerceValue     = null, CreateDefaultValueDelegate defaultValueCreator = null)
 {
     return(new BindableProperty(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue,
                                 defaultValueCreator: defaultValueCreator));
 }
示例#3
0
 public static BindablePropertyKey CreateReadOnly <TDeclarer, TPropertyType>(Expression <Func <TDeclarer, TPropertyType> > getter, TPropertyType defaultValue,
                                                                             BindingMode defaultBindingMode = BindingMode.OneWayToSource, ValidateValueDelegate <TPropertyType> validateValue = null,
                                                                             BindingPropertyChangedDelegate <TPropertyType> propertyChanged = null, BindingPropertyChangingDelegate <TPropertyType> propertyChanging = null,
                                                                             CoerceValueDelegate <TPropertyType> coerceValue = null, CreateDefaultValueDelegate <TDeclarer, TPropertyType> defaultValueCreator       = null) where TDeclarer : BindableObject
 {
     return(new BindablePropertyKey(Create(getter, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, null, true, defaultValueCreator)));
 }
示例#4
0
 /// <summary>
 /// Creates a new instance of the BindablePropertyKey class.
 /// </summary>
 /// <param name="propertyName">The name of the BindableProperty.</param>
 /// <param name="returnType">The type of the property.</param>
 /// <param name="declaringType">The type of the declaring object.</param>
 /// <param name="defaultValue">The default value for the property.</param>
 /// <param name="defaultBindingMode">The BindingMode to use on SetBinding() if no BindingMode is given. This parameter is optional. Default is BindingMode.OneWay.</param>
 /// <param name="validateValue">A delegate to be run when a value is set. This parameter is optional. Default is null.</param>
 /// <param name="propertyChanged">A delegate to be run when the value has changed. This parameter is optional. Default is null.</param>
 /// <param name="propertyChanging">A delegate to be run when the value will change. This parameter is optional. Default is null.</param>
 /// <param name="coerceValue">A delegate used to coerce the range of a value. This parameter is optional. Default is null.</param>
 /// <param name="defaultValueCreator">A Func used to initialize default value for reference types.</param>
 /// <returns>A newly created BindablePropertyKey.</returns>
 public static BindablePropertyKey CreateReadOnly(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWayToSource,
                                                  ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
                                                  CoerceValueDelegate coerceValue     = null, CreateDefaultValueDelegate defaultValueCreator = null)
 {
     return
         (new BindablePropertyKey(new BindableProperty(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue,
                                                       isReadOnly: true, defaultValueCreator: defaultValueCreator)));
 }
示例#5
0
 /// <summary>
 /// Creates a new instance of the BindableProperty class for an attached property.
 /// </summary>
 /// <param name="propertyName">The name of the BindableProperty.</param>
 /// <param name="returnType">The type of the property.</param>
 /// <param name="declaringType">The type of the declaring object.</param>
 /// <param name="defaultValue">The default value for the property.</param>
 /// <param name="defaultBindingMode">The BindingMode to use on SetBinding() if no BindingMode is given. This parameter is optional. Default is BindingMode.OneWay.</param>
 /// <param name="validateValue">A delegate to be run when a value is set. This parameter is optional. Default is null.</param>
 /// <param name="propertyChanged">A delegate to be run when the value has changed. This parameter is optional. Default is null.</param>
 /// <param name="propertyChanging">A delegate to be run when the value will change. This parameter is optional. Default is null.</param>
 /// <param name="coerceValue">A delegate used to coerce the range of a value. This parameter is optional. Default is null.</param>
 /// <param name="defaultValueCreator">A Func used to initialize default value for reference types.</param>
 /// <returns>A newly created BindableProperty.</returns>
 public static BindableProperty CreateAttached(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
                                               ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged = null, BindingPropertyChangingDelegate propertyChanging = null,
                                               CoerceValueDelegate coerceValue     = null, CreateDefaultValueDelegate defaultValueCreator = null)
 {
     return(CreateAttached(propertyName, returnType, declaringType, defaultValue, defaultBindingMode, validateValue, propertyChanged, propertyChanging, coerceValue, null, false, defaultValueCreator));
 }
示例#6
0
        BindableProperty(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
                         ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged  = null, BindingPropertyChangingDelegate propertyChanging = null,
                         CoerceValueDelegate coerceValue     = null, BindablePropertyBindingChanging bindingChanging = null, bool isReadOnly = false, CreateDefaultValueDelegate defaultValueCreator = null)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }
            if (ReferenceEquals(returnType, null))
            {
                throw new ArgumentNullException(nameof(returnType));
            }
            if (ReferenceEquals(declaringType, null))
            {
                throw new ArgumentNullException(nameof(declaringType));
            }

            // don't use Enum.IsDefined as its redonkulously expensive for what it does
            if (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime)
            {
                throw new ArgumentException("Not a valid type of BindingMode", nameof(defaultBindingMode));
            }
            if (defaultValue == null && Nullable.GetUnderlyingType(returnType) == null && returnType.GetTypeInfo().IsValueType)
            {
                throw new ArgumentException("Not a valid default value", nameof(defaultValue));
            }
            if (defaultValue != null && !returnType.IsInstanceOfType(defaultValue))
            {
                throw new ArgumentException("Default value did not match return type", nameof(defaultValue));
            }
            if (defaultBindingMode == BindingMode.Default)
            {
                defaultBindingMode = BindingMode.OneWay;
            }

            PropertyName        = propertyName;
            ReturnType          = returnType;
            ReturnTypeInfo      = returnType.GetTypeInfo();
            DeclaringType       = declaringType;
            DefaultValue        = defaultValue;
            DefaultBindingMode  = defaultBindingMode;
            PropertyChanged     = propertyChanged;
            PropertyChanging    = propertyChanging;
            ValidateValue       = validateValue;
            CoerceValue         = coerceValue;
            BindingChanging     = bindingChanging;
            IsReadOnly          = isReadOnly;
            DefaultValueCreator = defaultValueCreator;

            Dictionary <string, BindableProperty> nameToBindableProperty;

            bindablePropertyOfType.TryGetValue(declaringType, out nameToBindableProperty);
            if (null == nameToBindableProperty)
            {
                nameToBindableProperty = new Dictionary <string, BindableProperty>();
                bindablePropertyOfType.Add(declaringType, nameToBindableProperty);
            }

            if (!nameToBindableProperty.ContainsKey(propertyName))
            {
                nameToBindableProperty.Add(propertyName, this);
            }
            else
            {
                nameToBindableProperty[propertyName] = this;
            }
        }
示例#7
0
        BindableProperty(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingMode defaultBindingMode = BindingMode.OneWay,
                         ValidateValueDelegate validateValue = null, BindingPropertyChangedDelegate propertyChanged  = null, BindingPropertyChangingDelegate propertyChanging = null,
                         CoerceValueDelegate coerceValue     = null, BindablePropertyBindingChanging bindingChanging = null, bool isReadOnly = false, CreateDefaultValueDelegate defaultValueCreator = null)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }
            if (ReferenceEquals(returnType, null))
            {
                throw new ArgumentNullException(nameof(returnType));
            }
            if (ReferenceEquals(declaringType, null))
            {
                throw new ArgumentNullException(nameof(declaringType));
            }

            // don't use Enum.IsDefined as its redonkulously expensive for what it does
            if (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime)
            {
                throw new ArgumentException($"Not a valid type of BindingMode. Property: {returnType} {declaringType.Name}.{propertyName}. Default binding mode: {defaultBindingMode}", nameof(defaultBindingMode));
            }

            if (defaultValue == null && Nullable.GetUnderlyingType(returnType) == null && returnType.GetTypeInfo().IsValueType)
            {
                defaultValue = Activator.CreateInstance(returnType);
            }

            if (defaultValue != null && !returnType.IsInstanceOfType(defaultValue))
            {
                throw new ArgumentException($"Default value did not match return type. Property: {returnType} {declaringType.Name}.{propertyName} Default value type: {defaultValue.GetType().Name}, ", nameof(defaultValue));
            }

            if (defaultBindingMode == BindingMode.Default)
            {
                defaultBindingMode = BindingMode.OneWay;
            }

            PropertyName        = propertyName;
            ReturnType          = returnType;
            ReturnTypeInfo      = returnType.GetTypeInfo();
            DeclaringType       = declaringType;
            DefaultValue        = defaultValue;
            DefaultBindingMode  = defaultBindingMode;
            PropertyChanged     = propertyChanged;
            PropertyChanging    = propertyChanging;
            ValidateValue       = validateValue;
            CoerceValue         = coerceValue;
            BindingChanging     = bindingChanging;
            IsReadOnly          = isReadOnly;
            DefaultValueCreator = defaultValueCreator;
        }
示例#8
0
 /// <summary>
 /// Creates a new instance of the BindableProperty class.
 /// </summary>
 /// <param name="propertyName">The name of the BindableProperty.</param>
 /// <param name="returnType">The type of the property.</param>
 /// <param name="declaringType">The type of the declaring object.</param>
 /// <param name="defaultValue">The default value for the property.</param>
 /// <param name="propertyChanged">A delegate to be run when the value has changed. This parameter is optional. Default is null.</param>
 /// <param name="defaultValueCreator">A Func used to initialize default value for reference types.</param>
 /// <returns>A newly created BindableProperty.</returns>
 public static BindableProperty Create(string propertyName, Type returnType, Type declaringType, object defaultValue = null,
                                       BindingPropertyChangedDelegate propertyChanged = null,
                                       CreateDefaultValueDelegate defaultValueCreator = null)
 {
     return(new BindableProperty(propertyName, returnType, declaringType, defaultValue, propertyChanged: propertyChanged, defaultValueCreator: defaultValueCreator));
 }
示例#9
0
 BindableProperty(string propertyName, Type returnType, Type declaringType, object defaultValue, BindingPropertyChangedDelegate propertyChanged, CreateDefaultValueDelegate defaultValueCreator)
 {
     PropertyName        = propertyName;
     ReturnType          = returnType;
     DeclaringType       = declaringType;
     DefaultValue        = defaultValue;
     PropertyChanged     = propertyChanged;
     DefaultValueCreator = defaultValueCreator;
 }