protected virtual async void OnViewModelPropertyChanged(object sender, BindingEventArgs e) { var control = Controls.AsEnumerable().FirstOrDefault(x => x.Name == e.ControlName); if (control == null) { return; } //TODO: throw new MvfException($"Could not bind {e.Property.Name} property because related control is not found"); await BindingDispatcher.BindControl(control, e.Property, e.Converter, e.Type); }
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; } } }
protected virtual async Task InitializeStartupBindings() { foreach (var control in this.Controls.AsEnumerable()) { var controlBindingProperties = this.BindableProperties .Select(x => x.PropertyInfo) .Where(x => x.GetPropertyFromAttribute <MvfBindable, string>(y => y.ControlName) == control.Name) .ToList(); if (!controlBindingProperties.Any()) { continue; } foreach (var bindingProperty in controlBindingProperties) { await BindingDispatcher.BindControl(control, bindingProperty, bindingProperty.GetMvfConverterType()); } } }