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_PHONE || WINDOWS_WPF var newBinding = new System.Windows.Data.Binding #endif #if NETFX_CORE 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_PHONE FallbackValue = sourceStep.FallbackValue #endif }; BindingOperations.SetBinding(attachedObject, dependencyProperty, newBinding); }
private MvxFullBinding TestSetupCommon(IMvxValueConverter valueConverter, object converterParameter, object fallbackValue, Type targetType, out MockSourceBinding mockSource, out MockTargetBinding mockTarget) { ClearAll(); MvxBindingSingletonCache.Initialize(); 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 bindingDescription = new MvxBindingDescription { Source = new MvxPathSourceStepDescription() { Converter = valueConverter, ConverterParameter = converterParameter, FallbackValue = fallbackValue, SourcePropertyPath = sourceText, }, Mode = MvxBindingMode.TwoWay, TargetName = targetName }; mockSource = new MockSourceBinding(); mockTarget = new MockTargetBinding() { TargetType = targetType }; mockTarget.DefaultMode = MvxBindingMode.TwoWay; 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; }
private void TestCommon(MvxBindingMode bindingMode, bool expectSourceBinding, bool expectTargetBinding) { ClearAll(); MvxBindingSingletonCache.Initialize(); 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()); }
public MvxFullBinding(MvxBindingRequest bindingRequest) { _bindingDescription = bindingRequest.Description; CreateTargetBinding(bindingRequest.Target); CreateSourceBinding(bindingRequest.Source); }
public static void AddBinding(this IMvxBindingContextOwner view, object target, MvxBindingDescription bindingDescription, object clearKey = null) { var descriptions = new[] { bindingDescription }; view.AddBindings(target, descriptions, clearKey); }
public MvxBindingRequest(object source, object target, MvxBindingDescription description) { Target = target; Source = source; Description = description; }
public MvxFullBinding(MvxBindingRequest bindingRequest) { _bindingDescription = bindingRequest.Description; CreateTargetBinding(bindingRequest.Target); CreateSourceBinding(bindingRequest.Source); }