Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BindingResourceResolver"/> class.
 /// </summary>
 public BindingResourceResolver([NotNull] BindingResourceResolver resolver)
 {
     Should.NotBeNull(resolver, "resolver");
     _behaviors      = new Dictionary <string, Func <IDataContext, IList <string>, IBindingBehavior> >(resolver._behaviors);
     _converters     = new Dictionary <string, IBindingValueConverter>(resolver._converters);
     _dynamicMethods = new Dictionary <string, IBindingResourceMethod>(resolver._dynamicMethods);
     _objects        = new Dictionary <string, IBindingResourceObject>(resolver._objects);
     _types          = new Dictionary <string, Type>(resolver._types);
 }
Exemple #2
0
 public BindingResourceResolver([NotNull] BindingResourceResolver resolver)
 {
     Should.NotBeNull(resolver, "resolver");
     RootElementResourceName   = resolver.RootElementResourceName;
     BindingSourceResourceName = resolver.BindingSourceResourceName;
     SelfResourceName          = resolver.SelfResourceName;
     DataContextResourceName   = resolver.DataContextResourceName;
     _behaviors      = new Dictionary <string, Func <IDataContext, IList <object>, IBindingBehavior> >(resolver._behaviors);
     _converters     = new Dictionary <string, IBindingValueConverter>(resolver._converters);
     _dynamicMethods = new Dictionary <string, IBindingResourceMethod>(resolver._dynamicMethods);
     _objects        = new Dictionary <string, DynamicResourceObject>(resolver._objects);
     _types          = new Dictionary <string, Type>(resolver._types);
 }
        public void ParserShouldParseMultiExpressionWithConverter()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string converterName = "TestConverter";
            const string binding = "Text $TestConverter(SourceText1, SourceText2)";
            var target = new object();
            var args = new object[] { "tset", 1 };
            var sourceModel = new BindingSourceModel { IntProperty = int.MaxValue };
            var bindingMock = new DataBindingMock
            {
                TargetAccessor = new BindingSourceAccessorMock
                {
                    Source = new BindingSourceMock
                    {
                        GetPathMembers = b => new BindingPathMembersMock(this, BindingPath.Create("IntProperty"), new BindingMemberInfo("IntProperty", BindingSourceModel.IntPropertyInfo, typeof(BindingSourceModel)))
                    }
                }
            };

            var converterMock = new ValueConverterCoreMock
            {
                Convert = (o, type, arg3, arg4) =>
                {
                    type.ShouldEqual(BindingSourceModel.IntPropertyInfo.PropertyType);
                    o.ShouldEqual(args[0]);
                    arg3.ShouldEqual(args[1]);
                    return sourceModel;
                }
            };
            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddConverter(converterName, converterMock, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            context.Add(BindingConstants.Binding, bindingMock);

            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(sourceModel);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
        }
        public void ParserShouldParseMultiExpressionWithCustomObjectValueDynamic2()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string objectName = "TestObject";
            const string binding = "Text SourceText1 + SourceText2 == $TestObject.IntProperty";
            var targetObj = new object();
            var model = new BindingSourceModel { IntProperty = 10 };
            var args = new object[] { 9, 1, model.IntProperty };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddObject(objectName, new BindingResourceObject(model), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, targetObj, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(true);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(targetObj, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(targetObj, sources[1].Invoke(context), sourcePath2);
            BindingSourceShouldBeValid(sources[2].Invoke(context),
                GetMemberPath(model, sourceModel => sourceModel.IntProperty), model);
        }
        public void ParserShouldParseMultiExpressionWithCustomObjectValueDynamic1()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string sourcePath3 = "Item1";
            const string objectName = "TestObject";
            const string binding = "Text SourceText1 + SourceText2 == $TestObject.Item1";
            var target = new object();
            var tuple = new Tuple<int>(10);
            var expressionObject = new BindingResourceObject(tuple);
            var args = new object[] { 9, 1, tuple.Item1 };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddObject(objectName, expressionObject, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(true);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
            BindingSourceShouldBeValid(sources[2].Invoke(context), sourcePath3, tuple);
        }
        public void ParserShouldParseMultiExpressionWithCustomMethod()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string methodName = "TestMethod";
            const string binding = "Text $TestMethod(SourceText1, SourceText2).IntProperty";
            var target = new object();
            var args = new object[] { "tset", 1 };
            var sourceModel = new BindingSourceModel { IntProperty = int.MaxValue };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            var method = new BindingResourceMethod((list, objects, c) =>
            {
                objects[0].ShouldEqual(args[0]);
                objects[1].ShouldEqual(args[1]);
                return sourceModel;
            }, typeof(BindingSourceModel));
            resolver.AddMethod(methodName, method, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);


            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(sourceModel.IntProperty);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
        }
        public void ParserShouldParseConverterCultureFromResource()
        {
            const string binding = "Text Text, ConverterCulture=$param";
            var cultureInfo = new CultureInfo("ru-RU");
            var resolver = new BindingResourceResolver();
            resolver.AddObject("param", new BindingResourceObject(cultureInfo), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single());
            context.GetData(BindingBuilderConstants.ConverterCulture)
                .Invoke(EmptyContext)
                .ShouldEqual(cultureInfo);
        }
        public void ParserShouldParseCommandParameterFromResource()
        {
            const string value = "value";
            const string binding = "Text Text, CommandParameter=$param";
            var resolver = new BindingResourceResolver();
            resolver.AddObject("param", new BindingResourceObject(value), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single());
            context.GetData(BindingBuilderConstants.CommandParameter)
                .Invoke(EmptyContext)
                .ShouldEqual(value);
        }
        public void ParserShouldParseConverterName()
        {
            const string converterName = "test";
            const string binding = "Text SourceText, Converter=test";
            var value = new InverseBooleanValueConverter();
            var resolver = new BindingResourceResolver();
            resolver.AddConverter(converterName, value, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single());
            context.GetData(BindingBuilderConstants.Converter).Invoke(EmptyContext).ShouldEqual(value);
        }
        public void ParserShouldParseCustomBehaviorMultiParams()
        {
            const string behaviorName = "TestBehavior";
            const string binding = "Text SourceText, Behavior=TestBehavior(stringvalue, 10, null, true, part1.part2), Validate=true";
            var value = new TwoWayBindingMode();
            var resolver = new BindingResourceResolver();
            resolver.AddBehavior(behaviorName, (dataContext, list) =>
            {
                list.Count.ShouldEqual(5);
                list[0].ShouldEqual("stringvalue");
                list[1].ShouldEqual(10);
                list[2].ShouldBeNull();
                list[3].ShouldEqual(true);
                list[4].ShouldEqual("part1.part2");
                return value;
            }, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single());
            context.GetData(BindingBuilderConstants.Behaviors).OfType<TwoWayBindingMode>().Single().ShouldEqual(value);
        }
        public void ParserShouldParseCustomBehaviorByName2()
        {
            const string behaviorName = "TestBehavior";
            const string binding = "Text SourceText, TestBehavior=100";
            var value = new TwoWayBindingMode();
            var resolver = new BindingResourceResolver();
            resolver.AddBehavior(behaviorName, (dataContext, list) =>
            {
                list.Count.ShouldEqual(1);
                list[0].ShouldEqual(100);
                return value;
            }, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single());
            context.GetData(BindingBuilderConstants.Behaviors).Single().ShouldEqual(value);
        }
        public void ParserShouldParseMultiExpressionWithOneTimeScope()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string sourcePath3 = "SourceText3";
            const string methodName = "TestMethod";
            const string binding = "Text $OneTime($TestMethod(SourceText1, SourceText2).IntProperty) + SourceText3";

            const int firstValue = -1;
            int executionCount = 0;
            var target = new object();
            var args = new object[] { "tset", 1, 3 };
            var sourceModel = new BindingSourceModel { IntProperty = firstValue };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            var method = new BindingResourceMethod((list, objects, c) =>
            {
                ++executionCount;
                objects[0].ShouldEqual(args[0]);
                objects[1].ShouldEqual(args[1]);
                return sourceModel;
            }, typeof(BindingSourceModel));
            resolver.AddMethod(methodName, method, true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);


            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(firstValue + 3);
            sourceModel.IntProperty = int.MinValue;
            args[2] = 5;
            expression(context, args).ShouldEqual(firstValue + 5);
            args[2] = 6;
            expression(context, args).ShouldEqual(firstValue + 6);

            executionCount.ShouldEqual(1);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
            BindingSourceShouldBeValidDataContext(target, sources[2].Invoke(context), sourcePath3);
        }
        public void ParserShouldParseTargetNullValueFromResource()
        {
            const string value = "value";
            const string binding = "Text Text, TargetNullValue=$param";
            var resolver = new BindingResourceResolver();
            resolver.AddObject("param", new BindingResourceObject(value), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: new BindingProvider());
            BindingServiceProvider.ResourceResolver = resolver;

            var context = new BindingBuilder(bindingParser.Parse(new object(), binding, null, null).Single());
            context.GetData(BindingBuilderConstants.TargetNullValue)
                .ShouldEqual(value);
        }
        public void ParserShouldParseMultiExpressionWithCustomObjectValueStatic2()
        {
            const string targetPath = "Text";
            const string sourcePath1 = "SourceText1";
            const string sourcePath2 = "SourceText2";
            const string objectName = "TestObject";
            const string binding = "Text (SourceText1 + SourceText2).GetHashCode() == $$TestObject.GetHashCode()";
            var target = new object();
            var args = new object[] { 9, 1 };

            var provider = new BindingProvider();
            var resolver = new BindingResourceResolver();
            BindingServiceProvider.ResourceResolver = resolver;
            resolver.AddObject(objectName, new BindingResourceObject(10), true);
            IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider);

            var context = new BindingBuilder(bindingParser.Parse(target, binding, null, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var expression = context.GetData(BindingBuilderConstants.MultiExpression);
            expression(context, args).ShouldEqual(true);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1);
            BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="BindingResourceResolverEx" /> class.
 /// </summary>
 public BindingResourceResolverEx([NotNull] BindingResourceResolver resolver)
     : base(resolver)
 {
 }