public async Task <object> BindMvfProperty(PropertyInfo property, object value, Type converter) { var result = value.ChangeType(property.PropertyType); if (converter != null) { result = MvfValueConverter.GetConvertedBackValue(converter, result); } await Task.Run(() => property.SetValue(ViewModel, result)); return(value); }
private async void StartListeningForChanges() { while (true) { foreach (var control in this.Controls.AsEnumerable()) { var connectedBindings = this.BindableProperties.Where(x => x.PropertyInfo.GetPropertyFromAttribute <MvfBindable, string>(b => b.ControlName) == control.Name).ToList(); foreach (var controlBinding in connectedBindings) { var controlPropertyname = controlBinding.PropertyInfo.GetPropertyFromAttribute <MvfBindable, string>(b => b.ControlPropertyName); if (string.IsNullOrEmpty(controlPropertyname)) { continue; } var currentvalue = control.GetProperty(controlPropertyname).GetValue(control); var lastKnownValue = LastKnownValues[control][controlBinding.PropertyInfo]; if (currentvalue.DeserializedEquals(lastKnownValue)) { continue; } var value = await BindingDispatcher.BindMvfProperty(controlBinding.PropertyInfo, currentvalue, controlBinding.PropertyInfo.GetMvfConverterType()); if (controlBinding.PropertyInfo.GetMvfConverterType() != null) { value = MvfValueConverter.GetConvertedValue(controlBinding.PropertyInfo.GetMvfConverterType(), currentvalue); } LastKnownValues[control][controlBinding.PropertyInfo] = value; } } try { await Task.Delay(1, ChangesListenerTokenSource.Token); } catch (TaskCanceledException) { break; } } }
public async Task <bool> BindControl(Control control, PropertyInfo bindingProperty, Type converter = null, BindingType?type = null) { if (type != null && !CanBind(type.Value, bindingProperty)) { return(false); } var controlProprty = control.GetProperty(bindingProperty.GetPropertyFromAttribute <MvfBindable, string>(x => x.ControlPropertyName)); if (controlProprty == null) { throw new MvfException($"{bindingProperty.Name} is not known value of {control}"); } return(await Task.Factory.FromAsync(Invoke(), (result) => result.IsCompleted)); IAsyncResult Invoke() { return(control.BeginInvoke(new Action(() => { var value = bindingProperty.GetValue(ViewModel); //TODO ?? if (value == null) { return; } if (UpdateWithCustomUpdater(control, bindingProperty, value, converter)) { return; } if (converter != null) { value = MvfValueConverter.GetConvertedValue(converter, value); } controlProprty.SetValue(control, value.ChangeType(controlProprty.PropertyType)); }))); } }