/// <summary> /// Update a Label control. /// </summary> private static bool UpdateLabel(Label control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo propInfo, object data, IReflectionProperties <LitUiAttribute> dataProps) { var changed = false; if (attr != null) { switch (attr.Property) { case TargetProperty.None: break; case TargetProperty.Text: UpdatePropertyValue(control, controlProps, nameof(control.Text), data, true); break; } } if (dataProps != null) { foreach (var p in dataProps) { if (p.Value.Attribute.CtrlType != ControlType.None) { switch (p.Value.Attribute.Property) { } } } } return(changed); }
/// <summary> /// Setup a Label control. /// </summary> private static void SetupLabel(Label control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo propInfo) { if (attr != null) { UpdatePropertyValue(control, controlProps, nameof(control.Content), attr.Text, true); UpdatePropertyValue(control, controlProps, nameof(control.HorizontalAlignment), GetHorizontalOptions(attr.LayoutMode, attr.ContentLayout), true); UpdatePropertyValue(control, controlProps, nameof(control.VerticalAlignment), GetVerticalOptions(attr.LayoutMode, attr.ContentLayout), true); } }
/// <summary> /// Get a list of properties having a custom attribute with their values. /// </summary> public static IDictionary <string, object> GetPropertiesValues <TAttr>(IReflectionProperties <TAttr> props, object obj) where TAttr : class { var dict = new Dictionary <string, object>(); return(props?.Aggregate(dict, (d, p) => { d.Add(p.Key, p.Value.PropertyInfo.GetValue(obj)); return d; })); }
/// <summary> /// Update a control property's value usign reflection information. /// </summary> protected static bool UpdatePropertyValue(object control, IReflectionProperties props, string propName, object value, bool onlyIfNotNull) { if (!onlyIfNotNull || value != null) { if (props.TryGetValue(propName, out ReflectionProperty prop)) { var currValue = prop.PropertyInfo.GetValue(control, null); if (currValue != value) { prop.PropertyInfo.SetValue(control, value, null); return(true); } } } return(false); }
/// <summary> /// Get the properties of a generic type. /// </summary> public static IReflectionProperties <T> GetProperties(Type type) { IReflectionProperties <T> properties; lock (typesDict) { if (!typesDict.ContainsKey(type)) { properties = TypeHelper.GetPropertiesDict <T>(type); typesDict.Add(type, properties); } else { properties = typesDict[type]; } } return(properties); }
/// <summary> /// Set properties having a custom attribute with a dictionary value. /// </summary> public static IList <PropertyInfo> SetPropertiesValues <TAttr>(IReflectionProperties <TAttr> props, object obj, IDictionary <string, object> values, bool checkChanged = false) where TAttr : class { var changed = checkChanged ? new List <PropertyInfo>() : null; if (props != null) { foreach (var p in props) { if (values.TryGetValue(p.Key, out object value)) { if (SetPropertyValue(p.Value.PropertyInfo, obj, value, checkChanged)) { changed.Add(p.Value.PropertyInfo); } } } } return(changed); }
/// <summary> /// Setup a Button control. /// </summary> private static void SetupButton(Button control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo propInfo, object data, IReflectionProperties <LitUiAttribute> dataProps) { SetupCommon(control, controlProps, attr, propInfo); if (attr != null) { var text = attr.Text ?? attr.GetAutoCommandParameter(propInfo); UpdatePropertyValue(control, controlProps, nameof(control.Text), text, true); UpdatePropertyValue(control, controlProps, nameof(control.HorizontalOptions), GetHorizontalOptions(attr.LayoutMode, attr.ContentLayout), true); UpdatePropertyValue(control, controlProps, nameof(control.VerticalOptions), GetVerticalOptions(attr.LayoutMode, attr.ContentLayout), true); control.Clicked += delegate(object sender, EventArgs e) { if (!string.IsNullOrEmpty(attr.OnClickCommand)) { if (dataProps.ContainsKey(attr.OnClickCommand)) { var cmdPropInfo = dataProps[attr.OnClickCommand].PropertyInfo; var parameter = attr.CommandParameter ?? attr.GetAutoCommandParameter(propInfo); var ev = new ControlEvent { Sender = sender, CommandName = attr.OnClickCommand, CommandParameter = parameter, Property = dataProps[propInfo.Name] }; cmdPropInfo.SetValue(data, ev, null); } } }; } }
/// <summary> /// Set properties having a custom attribute with a dictionary value. /// </summary> public static bool SetPropertyValue <TAttr>(IReflectionProperties <TAttr> props, string propName, object obj, object value, bool checkChanged = false) where TAttr : class { var item = props?.FirstOrDefault(i => i.Value.PropertyInfo.Name == propName); return(SetPropertyValue(item?.Value.PropertyInfo, obj, value, checkChanged)); }
protected override bool UpdateControl(bool setup, ControlType?controlType, View control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo propInfo, object data, IReflectionProperties <LitUiAttribute> dataProps) { switch (controlType) { case ControlType.None: return(false); case ControlType.Label: { if (control is Label label) { if (setup) { SetupLabel(label, controlProps, attr, propInfo); } return(UpdateLabel(label, controlProps, attr, propInfo, data, dataProps)); } return(false); } case ControlType.Button: { if (control is Button button) { if (setup) { SetupButton(button, controlProps, attr, propInfo, data, dataProps); } return(UpdateButton(button, controlProps, data, dataProps)); } return(false); } case ControlType.Container: { if (control is Layout <View> container) { if (setup) { SetupContainer(container, controlProps, attr, propInfo, data, dataProps); } return(UpdateContainer(container, controlProps, data, dataProps)); } return(false); } default: return(false); } }
/// <summary> /// Common setup for a control. /// </summary> private static void SetupCommon(View control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo propInfo) { if (attr != null) { } }
/// <summary> /// Update a container control. /// </summary> private static bool UpdateContainer(Layout <View> control, IReflectionProperties controlProps, object data, IReflectionProperties <LitUiAttribute> dataProps) { var changed = false; return(changed); }
/// <summary> /// Setup a container control. /// </summary> private static void SetupContainer(Layout <View> control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo containerPropInfo, object data, IReflectionProperties <LitUiAttribute> dataProps) { SetupCommon(control, controlProps, attr, containerPropInfo); if (dataProps != null) { for (var step = 1; step <= 2; step++) { foreach (var pair in dataProps) { var reflectionProperty = pair.Value; var propInfo = reflectionProperty.PropertyInfo; var propAttr = reflectionProperty.Attribute; switch (step) { case 1: { if (propAttr.HasGridSpecification && control is Grid grid) { var maxCol = propAttr.Col + (propAttr.ColSpan > 0 ? propAttr.ColSpan - 1 : 0); while (grid.ColumnDefinitions.Count <= maxCol) { grid.ColumnDefinitions.Add(new ColumnDefinition()); } var maxRow = propAttr.Row + (propAttr.RowSpan > 0 ? propAttr.RowSpan - 1 : 0); while (grid.RowDefinitions.Count <= maxRow) { grid.RowDefinitions.Add(new RowDefinition()); } } } break; case 2: { var propValue = propInfo.GetValue(data, null); var isVisualProperty = TypeHelper.IsSubclassOf(propInfo.PropertyType, typeof(VisualProperty <>)); if (isVisualProperty) { if (propValue == null) { propValue = Activator.CreateInstance(propInfo.PropertyType); propInfo.SetValue(data, propValue, null); } } var visualProp = isVisualProperty ? propValue as VisualProperty : null; var childData = isVisualProperty ? visualProp.GetData() : propAttr.OwnDataContext ? propValue : data; var child = CreateControl(control, propAttr, propInfo, childData); if (isVisualProperty) { visualProp.Bind(child); visualProp.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e) { UpdateControl(sender as View, propAttr, propInfo, visualProp.GetData(), false); }; } } break; } } } } }
/// <summary> /// Update a Button control. /// </summary> private static bool UpdateButton(Button control, IReflectionProperties controlProps, object data, IReflectionProperties <LitUiAttribute> dataProps) { var changed = false; return(changed); }
/// <summary> /// Updates (optionally also setup) a control. /// </summary> protected abstract bool UpdateControl(bool setup, ControlType?controlType, TControl control, IReflectionProperties controlProps, LitUiAttribute attr, PropertyInfo propInfo, object data, IReflectionProperties <LitUiAttribute> dataProps);
/// <summary> /// Constructor. /// </summary> public ReflectionClass() { var type = GetType(); properties = GetProperties(type); }