protected virtual void ApplyBinding(MvxBindingDescription bindingDescription, Type actualType, FrameworkElement attachedObject) { DependencyProperty dependencyProperty = actualType.FindDependencyProperty(bindingDescription.TargetName); if (dependencyProperty == null) { Mvx.Warning("Dependency property not found for {0}", bindingDescription.TargetName); return; } var property = actualType.FindActualProperty(bindingDescription.TargetName); if (property == null) { Mvx.Warning("Property not returned {0} - may cause issues", bindingDescription.TargetName); } var sourceStep = bindingDescription.Source as MvxPathSourceStepDescription; if (sourceStep == null) { Mvx.Warning("Binding description for {0} is not a simple path - Windows Binding cannot cope with this", bindingDescription.TargetName); return; } #if WINDOWS_WPF var newBinding = new System.Windows.Data.Binding #endif #if WINDOWS_COMMON var newBinding = new Windows.UI.Xaml.Data.Binding #endif { Path = new PropertyPath(sourceStep.SourcePropertyPath), Mode = ConvertMode(bindingDescription.Mode, property?.PropertyType ?? typeof(object)), Converter = GetConverter(sourceStep.Converter), ConverterParameter = sourceStep.ConverterParameter, #if WINDOWS_COMMON FallbackValue = sourceStep.FallbackValue #endif }; BindingOperations.SetBinding(attachedObject, dependencyProperty, newBinding); }
public static void AddBinding(this IMvxBindingContextOwner view, object target, MvxBindingDescription bindingDescription, object clearKey = null) { var descriptions = new[] { bindingDescription }; view.AddBindings(target, descriptions, clearKey); }
public MvxFullBinding(MvxBindingRequest bindingRequest) { this._bindingDescription = bindingRequest.Description; this.CreateTargetBinding(bindingRequest.Target); this.CreateSourceBinding(bindingRequest.Source); }
private MvxFullBinding TestSetupCommon(MvxBindingMode mvxBindingMode, MvxBindingMode defaultMode, out MockSourceBinding mockSource, out MockTargetBinding mockTarget) { ClearAll(); MvxBindingSingletonCache.Initialize(); Ioc.RegisterSingleton<IMvxMainThreadDispatcher>(new InlineMockMainThreadDispatcher()); var mockSourceBindingFactory = new Mock<IMvxSourceBindingFactory>(); Ioc.RegisterSingleton(mockSourceBindingFactory.Object); var mockTargetBindingFactory = new Mock<IMvxTargetBindingFactory>(); Ioc.RegisterSingleton(mockTargetBindingFactory.Object); var realSourceStepFactory = new MvxSourceStepFactory(); realSourceStepFactory.AddOrOverwrite(typeof(MvxPathSourceStepDescription), new MvxPathSourceStepFactory()); Ioc.RegisterSingleton<IMvxSourceStepFactory>(realSourceStepFactory); var sourceText = "sourceText"; var targetName = "targetName"; var source = new { Value = 1 }; var target = new { Value = 2 }; var converterParameter = new { Value = 3 }; var fallbackValue = new { Value = 4 }; IMvxValueConverter converter = null; var bindingDescription = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { Converter = converter, ConverterParameter = converterParameter, FallbackValue = fallbackValue, SourcePropertyPath = sourceText, }, Mode = mvxBindingMode, TargetName = targetName }; mockSource = new MockSourceBinding(); mockTarget = new MockTargetBinding(); mockTarget.DefaultMode = defaultMode; var localSource = mockSource; mockSourceBindingFactory .Setup(x => x.CreateBinding(It.IsAny<object>(), It.Is<string>(s => s == sourceText))) .Returns((object a, string b) => localSource); var localTarget = mockTarget; mockTargetBindingFactory .Setup(x => x.CreateBinding(It.IsAny<object>(), It.Is<string>(s => s == targetName))) .Returns((object a, string b) => localTarget); mockSource.TryGetValueResult = true; mockSource.TryGetValueValue = "TryGetValueValue"; var request = new MvxBindingRequest(source, target, bindingDescription); var toTest = new MvxFullBinding(request); return toTest; }
public MvxFullBinding(MvxBindingRequest bindingRequest) { this._bindingDescription = bindingRequest.Description; this.CreateTargetBinding(bindingRequest.Target); this.CreateSourceBinding(bindingRequest.Source); }
public MvxBindingRequest(object source, object target, MvxBindingDescription description) { this.Target = target; this.Source = source; this.Description = description; }
private void TestCommon(MvxBindingMode bindingMode, bool expectSourceBinding, bool expectTargetBinding) { ClearAll(); MvxBindingSingletonCache.Initialize(); Ioc.RegisterSingleton<IMvxMainThreadDispatcher>(new InlineMockMainThreadDispatcher()); var mockSourceBindingFactory = new Mock<IMvxSourceBindingFactory>(); Ioc.RegisterSingleton(mockSourceBindingFactory.Object); var mockTargetBindingFactory = new Mock<IMvxTargetBindingFactory>(); Ioc.RegisterSingleton(mockTargetBindingFactory.Object); var realSourceStepFactory = new MvxSourceStepFactory(); realSourceStepFactory.AddOrOverwrite(typeof(MvxPathSourceStepDescription), new MvxPathSourceStepFactory()); Ioc.RegisterSingleton<IMvxSourceStepFactory>(realSourceStepFactory); var sourceText = "sourceText"; var targetName = "targetName"; var source = new { Value = 1 }; var target = new { Value = 2 }; var converterParameter = new { Value = 3 }; var fallbackValue = new { Value = 4 }; var converter = new Mock<IMvxValueConverter>(); var bindingDescription = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { Converter = converter.Object, ConverterParameter = converterParameter, FallbackValue = fallbackValue, SourcePropertyPath = sourceText, }, Mode = bindingMode, TargetName = targetName }; var mockSourceBinding = new Mock<IMvxSourceBinding>(); var mockTargetBinding = new Mock<IMvxTargetBinding>(); mockSourceBindingFactory .Setup(x => x.CreateBinding(It.Is<object>(s => s == source), It.Is<string>(s => s == sourceText))) .Returns((object a, string b) => mockSourceBinding.Object); mockTargetBindingFactory .Setup(x => x.CreateBinding(It.Is<object>(s => s == target), It.Is<string>(s => s == targetName))) .Returns((object a, string b) => mockTargetBinding.Object); var request = new MvxBindingRequest(source, target, bindingDescription); var toTest = new MvxFullBinding(request); //var sourceBindingTimes = expectSourceBinding ? Times.Once() : Times.Never(); //mockSourceBinding.Verify(x => x.Changed += It.IsAny<EventHandler<MvxSourcePropertyBindingEventArgs>>(), sourceBindingTimes); mockSourceBindingFactory .Verify(x => x.CreateBinding(It.Is<object>(s => s == source), It.Is<string>(s => s == sourceText)), Times.Once()); //var targetBindingTimes = expectSourceBinding ? Times.Once() : Times.Never(); //mockTargetBinding.Verify(x => x.ValueChanged += It.IsAny<EventHandler<MvxTargetChangedEventArgs>>(), targetBindingTimes); mockTargetBindingFactory .Verify(x => x.CreateBinding(It.Is<object>(s => s == target), It.Is<string>(s => s == targetName)), Times.Once()); }
private void DoTest( Action<MockBindingContext> action, Func<MockBindingContext, object> findTargetObjectFunc, MvxBindingDescription expectedDescription) { ClearAll(); MvxBindingSingletonCache.Initialize(); var dataContext = new TestDataContext(); var bindingContext = new Mock<IMvxBindingContext>(); bindingContext.Setup(x => x.RegisterBinding(It.IsAny<object>(), It.IsAny<IMvxUpdateableBinding>())); bindingContext.SetupGet(x => x.DataContext).Returns(dataContext); var callbacksSeen = new List<Callback>(); var binder = new Mock<IMvxBinder>(); binder.Setup( b => b.Bind(It.IsAny<object>(), It.IsAny<object>(), It.IsAny<IEnumerable<MvxBindingDescription>>())) .Callback((object source, object target, IEnumerable<MvxBindingDescription> descriptions) => { if (descriptions.Count() != 1) throw new Exception("Unexpected description count"); callbacksSeen.Add(new Callback { Source = source, Target = target, BindingDescription = descriptions.First() }); }); Ioc.RegisterSingleton(binder.Object); Ioc.RegisterSingleton<IMvxPropertyExpressionParser>(new MvxPropertyExpressionParser()); var converterLookup = new MvxValueConverterRegistry(); converterLookup.AddOrOverwrite("SampleConv", new SampleValueConverter()); Ioc.RegisterSingleton<IMvxValueConverterLookup>(converterLookup); var testTarget = new MockBindingContext { Target = new TestTarget(), BindingContext = bindingContext.Object }; action(testTarget); Assert.AreEqual(1, callbacksSeen.Count); var callback = callbacksSeen[0]; var expectedTarget = findTargetObjectFunc(testTarget); Assert.AreEqual(expectedTarget, callback.Target); Assert.AreEqual(dataContext, callback.Source); var desc = callback.BindingDescription; Assert.IsTrue(expectedDescription.Source is MvxPathSourceStepDescription); var path = desc.Source as MvxPathSourceStepDescription; Assert.IsTrue(desc.Source is MvxPathSourceStepDescription); var expectedPath = expectedDescription.Source as MvxPathSourceStepDescription; Assert.AreEqual(expectedPath.ConverterParameter, path.ConverterParameter); Assert.AreEqual(expectedPath.FallbackValue, path.FallbackValue); Assert.AreEqual(expectedPath.SourcePropertyPath, path.SourcePropertyPath); Assert.AreEqual(expectedDescription.Mode, desc.Mode); Assert.AreEqual(expectedDescription.TargetName, desc.TargetName); if (expectedPath.Converter == null) Assert.IsNull(path.Converter); else Assert.AreEqual(expectedPath.Converter.GetType(), path.Converter.GetType()); }
public void TestLongExpression() { var expectedDesc = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { SourcePropertyPath = "MyCollection.GrandParent.MyChild.MyChild.Value", }, TargetName = "Text" }; Action<MockBindingContext> test = mock => mock .CreateBinding(mock.Target) .For(te => te.Text) .To<TestDataContext>(source => source.MyCollection.GrandParent.MyChild.MyChild.Value) .Apply(); this.DoTest(test, expectedDesc); }
private void DoTest( Action<MockBindingContext> action, MvxBindingDescription expectedDescription) { this.DoTest(action, (context) => context.Target, expectedDescription); }
public void TestDirectToObjectExpression() { var expectedDesc = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { SourcePropertyPath = "MyCollection.MyLookup[\"Fred\"].Value", }, TargetName = "SimpleValue" }; Action<MockBindingContext> test = mock => mock .CreateBinding() .For(te => te.SimpleValue) .To<TestDataContext>(source => source.MyCollection.MyLookup["Fred"].Value) .Apply(); this.DoTest(test, mock => mock, expectedDesc); }
public void TestStringVariableIndexedExpression() { var expectedDesc = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { SourcePropertyPath = "MyCollection.MyLookup[\"Fred\"].Value", }, TargetName = "Text" }; var index = "Fred"; Action<MockBindingContext> test = mock => mock .CreateBinding(mock.Target) .For(te => te.Text) .To<TestDataContext>(source => source.MyCollection.MyLookup[index].Value) .Apply(); this.DoTest(test, expectedDesc); }
public void TestNumberPropertyIndexedExpression() { var expectedDesc = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { SourcePropertyPath = "MyCollection.MyList[0].Value", }, TargetName = "Text" }; var obj = new { Index = 0 }; Action<MockBindingContext> test = mock => mock .CreateBinding(mock.Target) .For(te => te.Text) .To<TestDataContext>(source => source.MyCollection.MyList[obj.Index].Value) .Apply(); DoTest(test, expectedDesc); }
public void TestExtraParametersStringConverter() { var expectedDesc = new MvxBindingDescription { TargetName = "Text", Source = new MvxPathSourceStepDescription() { SourcePropertyPath = "MyCollection.GrandParent.MyChild.MyChild.Value", ConverterParameter = "My Converter Parameter", Converter = new SampleValueConverter(), FallbackValue = 12.3445, }, Mode = MvxBindingMode.TwoWay }; Action<MockBindingContext> test = mock => mock .CreateBinding(mock.Target) .For(te => te.Text) .To<TestDataContext>(source => source.MyCollection.GrandParent.MyChild.MyChild.Value) .WithConversion(new SampleValueConverter(), "My Converter Parameter") .WithFallback(12.3445) .TwoWay() .Apply(); this.DoTest(test, expectedDesc); }