示例#1
0
        static bool TryConvertBool <T>(object prop, out T result)
        {
            result = default(T);

            var stringToBoolConverter = new StringToBoolConverter();
            var floatToBoolConverter  = new FloatToBoolConverter();

            if (stringToBoolConverter.CanConvertTo(prop, typeof(bool)))
            {
                result = (T)stringToBoolConverter.ConvertTo(prop, typeof(bool));
                return(true);
            }

            if (floatToBoolConverter.CanConvertTo(prop, typeof(bool)))
            {
                result = (T)floatToBoolConverter.ConvertTo(prop, typeof(bool));
                return(true);
            }

            return(false);
        }
        public T GetProperty <T> (BindableProperty formProperty)
        {
            Tuple <string[], bool> property = formProperty.GetPlatformPropertyQuery();

            string[] propertyPath       = property.Item1;
            bool     isOnParentRenderer = property.Item2;

            var query = ViewQuery;

            if (isOnParentRenderer &&
                PlatformViewType != PlatformViews.BoxView &&
                PlatformViewType != PlatformViews.Frame)
            {
                query = query + " parent * index:0";
            }

            object prop  = null;
            bool   found = false;

            bool isEdgeCase = false;

#if __ANDROID__
            isEdgeCase = (formProperty == View.ScaleProperty);
#endif
            if (!isEdgeCase)
            {
                found =
                    MaybeGetProperty <string> (App, query, propertyPath, out prop) ||
                    MaybeGetProperty <float> (App, query, propertyPath, out prop) ||
                    MaybeGetProperty <bool> (App, query, propertyPath, out prop) ||
                    MaybeGetProperty <object> (App, query, propertyPath, out prop);
            }

#if __ANDROID__
            if (formProperty == View.ScaleProperty)
            {
                var matrix = new Matrix();
                matrix.M00 = App.Query(q => q.Raw(query).Invoke(propertyPath[0]).Value <float> ()).First();
                matrix.M11 = App.Query(q => q.Raw(query).Invoke(propertyPath[1]).Value <float> ()).First();
                matrix.M22 = 0.5f;
                matrix.M33 = 1.0f;
                return((T)((object)matrix));
            }
#endif

            if (!found || prop == null)
            {
                throw new NullReferenceException("null property");
            }

            if (prop.GetType() == typeof(T))
            {
                return((T)prop);
            }

            if (prop.GetType() == typeof(string) && typeof(T) == typeof(Matrix))
            {
                Matrix matrix = ParsingUtils.ParseCATransform3D((string)prop);
                return((T)((object)matrix));
            }

            if (typeof(T) == typeof(Color))
            {
#if __IOS__
                Color color = ParsingUtils.ParseUIColor((string)prop);
                return((T)((object)color));
#else
                uint  intColor = (uint)((float)prop);
                Color color    = Color.FromUint(intColor);
                return((T)((object)color));
#endif
            }

#if __IOS__
            if (prop.GetType() == typeof(string) && typeof(T) == typeof(Font))
            {
                Font font = ParsingUtils.ParseUIFont((string)prop);
                return((T)((object)font));
            }
#endif

            T result = default(T);

            var stringToBoolConverter = new StringToBoolConverter();
            var floatToBoolConverter  = new FloatToBoolConverter();

            if (stringToBoolConverter.CanConvertTo(prop, typeof(bool)))
            {
                result = (T)stringToBoolConverter.ConvertTo(prop, typeof(bool));
            }
            else if (floatToBoolConverter.CanConvertTo(prop, typeof(bool)))
            {
                result = (T)floatToBoolConverter.ConvertTo(prop, typeof(bool));
            }

            return(result);
        }
        public T GetProperty <T>(BindableProperty formProperty)
        {
#if __ANDROID__
#endif

            Tuple <string[], bool> property = formProperty.GetPlatformPropertyQuery();
            string[] propertyPath           = property.Item1;
            bool     isOnParentRenderer     = property.Item2;

            var query = ViewQuery;
            if (isOnParentRenderer &&
                PlatformViewType != PlatformViews.BoxView &&
                PlatformViewType != PlatformViews.Frame)
            {
#if __ANDROID__
                // If we're testing the fast renderers, we don't want to check the parent control for
                // this property (despite `isOnParentRenderer` being true); if we're testing a legacy
                // renderer, then we *do* need to check the parent control for the property
                // So we query the control's parent and see if it's a Container (legacy); if so,
                // we adjust the query to look at the parent of the current control
                var parent = App.Query(appQuery => appQuery.Raw(ViewQuery + " parent * index:0"));
                if (parent.Length > 0 && parent[0].Label.EndsWith(ContainerLabel))
                {
                    query = query + " parent * index:0";
                }
#else
                query = query + " parent * index:0";
#endif
            }

            object prop  = null;
            bool   found = false;

            bool isEdgeCase = false;
#if __ANDROID__
            isEdgeCase = (formProperty == View.ScaleProperty);
#endif
            if (!isEdgeCase)
            {
                found =
                    MaybeGetProperty <string>(App, query, propertyPath, out prop) ||
                    MaybeGetProperty <float>(App, query, propertyPath, out prop) ||
                    MaybeGetProperty <bool>(App, query, propertyPath, out prop) ||
                    MaybeGetProperty <object>(App, query, propertyPath, out prop);
            }
#if __MACOS__
            if (!found)
            {
                if (formProperty == View.IsEnabledProperty)
                {
                    var view = App.Query((arg) => arg.Raw(query)).FirstOrDefault();
                    found = view != null;
                    prop  = view.Enabled;
                }

                if (formProperty == Button.TextProperty)
                {
                    var view = App.Query((arg) => arg.Raw(query)).FirstOrDefault();
                    found = view != null;
                    prop  = view.Text;
                }
            }
#endif

#if __ANDROID__
            if (formProperty == View.ScaleProperty)
            {
                var matrix = new Matrix();
                matrix.M00 = App.Query(q => q.Raw(query).Invoke(propertyPath[0]).Value <float> ()).First();
                matrix.M11 = App.Query(q => q.Raw(query).Invoke(propertyPath[1]).Value <float> ()).First();
                matrix.M22 = 0.5f;
                matrix.M33 = 1.0f;
                return((T)((object)matrix));
            }
#endif

            if (!found || prop == null)
            {
                throw new NullReferenceException("null property");
            }

            if (prop.GetType() == typeof(T))
            {
                return((T)prop);
            }

            if (prop.GetType() == typeof(string) && typeof(T) == typeof(Matrix))
            {
                Matrix matrix = ParsingUtils.ParseCATransform3D((string)prop);
                return((T)((object)matrix));
            }

            if (typeof(T) == typeof(Color))
            {
#if __IOS__
                Color color = ParsingUtils.ParseUIColor((string)prop);
                return((T)((object)color));
#else
                uint  intColor = (uint)((float)prop);
                Color color    = Color.FromUint(intColor);
                return((T)((object)color));
#endif
            }

#if __IOS__
            if (prop.GetType() == typeof(string) && typeof(T) == typeof(Font))
            {
                Font font = ParsingUtils.ParseUIFont((string)prop);
                return((T)((object)font));
            }
#endif

            T result = default(T);

            var stringToBoolConverter = new StringToBoolConverter();
            var floatToBoolConverter  = new FloatToBoolConverter();

            if (stringToBoolConverter.CanConvertTo(prop, typeof(bool)))
            {
                result = (T)stringToBoolConverter.ConvertTo(prop, typeof(bool));
            }
            else if (floatToBoolConverter.CanConvertTo(prop, typeof(bool)))
            {
                result = (T)floatToBoolConverter.ConvertTo(prop, typeof(bool));
            }

            return(result);
        }