Esempio n. 1
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Expression == null)
            {
                return(0.0);
            }
            var nameResolver = (IXamlNameResolver)serviceProvider;

            if (nameResolver == null)
            {
                throw new ArgumentException("Missing IXamlNameResolver", "context");
            }

            List <string> unknownNameList;
            var           nameValueTable = ExpressionServices.GetNameValueTable(nameResolver, Expression, out unknownNameList);

            if (unknownNameList != null && unknownNameList.Count > 0)
            {
                object fixup = nameResolver.GetFixupToken(unknownNameList);
                return(fixup);
            }

            // if we have the values of all the names,
            // hand it to the expresssion parser and let 'er rip!
            //
            MathExpressionParser parser = new MathExpressionParser(nameValueTable);
            Double number = parser.Parse(Expression);

            return(number);
        }
Esempio n. 2
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string text = (string)value;

            if (text == null)
            {
                throw new ArgumentException("Missing String Value", "value");
            }
            var nameResolver = (IXamlNameResolver)context;

            if (nameResolver == null)
            {
                throw new ArgumentException("Missing IXamlNameResolver", "context");
            }


            var names   = new List <string>();
            var scanner = new MathExpressionScanner(text);

            foreach (MathExpressionToken scannerTok in scanner.Scan())
            {
                switch (scannerTok.TokenType)
                {
                case MathTokenType.Name:
                    if (!names.Contains(scannerTok.Name))
                    {
                        names.Add(scannerTok.Name);
                    }
                    break;
                }
            }

            var unknownNameList = new List <string>();
            var nameValueTable  = ExpressionServices.GetNameValueTable(nameResolver, text, out unknownNameList);

            if (unknownNameList != null && unknownNameList.Count > 0)
            {
                object fixup = nameResolver.GetFixupToken(unknownNameList);
                return(fixup);
            }

            // if we have the values of all the names,
            // hand it to the expresssion parser and let 'er rip!
            //
            MathExpressionParser parser = new MathExpressionParser(nameValueTable);
            Double number = parser.Parse(text);

            return(number);
        }