Example #1
0
        public FlexLength(double value, FlexLengthUnitType type)
        {
            //if (DoubleUtils.IsNaN(value))
            //  throw new ArgumentException(System.Windows.SR.Get("InvalidCtorParameterNoNaN", (object) "value"));

            //if (double.IsInfinity(value))
            //  throw new ArgumentException(System.Windows.SR.Get("InvalidCtorParameterNoInfinity", (object) "value"));

            //if (type != FlexLengthUnitType.Auto && type != FlexLengthUnitType.Pixel && type != FlexLengthUnitType.Star)
            //  throw new ArgumentException(System.Windows.SR.Get("InvalidCtorParameterUnknownFlexLengthUnitType", (object) "type"));

            _unitValue = type == FlexLengthUnitType.Auto ? 0.0 : value;
            UnitType   = type;
        }
Example #2
0
        private static void FromString(string s, CultureInfo cultureInfo, out double value, out FlexLengthUnitType unit)
        {
            var normalized = s.Trim().ToLowerInvariant();

            unit = FlexLengthUnitType.Pixel;

            var strLen     = normalized.Length;
            var strLenUnit = 0;
            var unitFactor = 1.0;

            var i = 0;

            if (normalized == UnitStrings[i])
            {
                strLenUnit = UnitStrings[i].Length;
                unit       = (FlexLengthUnitType)i;
            }
            else
            {
                for (i = 1; i < UnitStrings.Length; ++i)
                {
                    if (normalized.EndsWith(UnitStrings[i], StringComparison.Ordinal) == false)
                    {
                        continue;
                    }

                    strLenUnit = UnitStrings[i].Length;
                    unit       = (FlexLengthUnitType)i;

                    break;
                }
            }

            if (i >= UnitStrings.Length)
            {
                for (i = 0; i < PixelUnitStrings.Length; ++i)
                {
                    if (normalized.EndsWith(PixelUnitStrings[i], StringComparison.Ordinal) == false)
                    {
                        continue;
                    }

                    strLenUnit = PixelUnitStrings[i].Length;
                    unitFactor = PixelUnitFactors[i];

                    break;
                }
            }

            if (strLen == strLenUnit && (unit == FlexLengthUnitType.Auto || unit == FlexLengthUnitType.Star))
            {
                value = 1;
            }
            else
            {
                Debug.Assert(unit == FlexLengthUnitType.Pixel || DoubleUtils.AreClose(unitFactor, 1.0));

                var valueString = normalized.Substring(0, strLen - strLenUnit);

                value = Convert.ToDouble(valueString, cultureInfo) * unitFactor;
            }
        }