コード例 #1
0
        private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>
        {
            var fieldType = item.PropertyType;
            if (fieldType != null && Model != null)
            {
                // GetDisplayName
                var fieldName   = item.GetFieldName();
                var displayName = Utility.GetDisplayName(Model, fieldName);

                // FieldValue
                var valueInvoker = GetPropertyValueLambdaCache.GetOrAdd((typeof(TModel), fieldName), key => LambdaExtensions.GetPropertyValueLambda <TModel, object?>(Model, key.FieldName).Compile());
                var fieldValue   = valueInvoker.Invoke(Model);

                // ValueChanged
                var valueChangedInvoker = CreateLambda(fieldType).Compile();
                var fieldValueChanged   = valueChangedInvoker(Model, fieldName);

                // ValueExpression
                var body            = Expression.Property(Expression.Constant(Model), typeof(TModel), fieldName);
                var tDelegate       = typeof(Func <>).MakeGenericType(fieldType);
                var valueExpression = Expression.Lambda(tDelegate, body);

                var index         = 0;
                var componentType = EditorForm <TModel> .GenerateComponent(fieldType);

                builder.OpenComponent(index++, componentType);
                builder.AddAttribute(index++, "DisplayText", displayName);
                builder.AddAttribute(index++, "Value", fieldValue);
                builder.AddAttribute(index++, "ValueChanged", fieldValueChanged);
                builder.AddAttribute(index++, "ValueExpression", valueExpression);
                builder.AddAttribute(index++, "IsDisabled", item.Readonly);
                builder.AddMultipleAttributes(index++, CreateMultipleAttributes(fieldType, fieldName, item));
                builder.CloseComponent();
            }
        };
コード例 #2
0
        private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>
        {
            var fieldType = item.PropertyType;
            if (fieldType != null && Model != null)
            {
                var fieldName = item.GetFieldName();
                // GetDisplayName
                // 先取 Text 属性值,然后取资源文件中的值
                var displayName = item.GetDisplayName() ?? Utility.GetDisplayName(Model, fieldName);

                // FieldValue
                var valueInvoker = GetPropertyValueLambdaCache.GetOrAdd((typeof(TModel), fieldName), key => LambdaExtensions.GetPropertyValueLambda <TModel, object?>(Model, key.FieldName).Compile());
                var fieldValue   = valueInvoker.Invoke(Model);

                // ValueChanged
                var valueChangedInvoker = CreateLambda(fieldType).Compile();
                var fieldValueChanged   = valueChangedInvoker(Model, fieldName);

                // ValueExpression
                var body            = Expression.Property(Expression.Constant(Model), typeof(TModel), fieldName);
                var tDelegate       = typeof(Func <>).MakeGenericType(fieldType);
                var valueExpression = Expression.Lambda(tDelegate, body);

                if (IsDisplay)
                {
                    builder.OpenComponent(0, typeof(Display <>).MakeGenericType(fieldType));
                    builder.AddAttribute(1, "DisplayText", displayName);
                    builder.AddAttribute(2, "Value", fieldValue);
                    builder.AddAttribute(3, "ValueChanged", fieldValueChanged);
                    builder.AddAttribute(4, "ValueExpression", valueExpression);
                    builder.AddAttribute(5, "ShowLabel", ShowLabel ?? true);
                    builder.CloseComponent();
                }
                else
                {
                    var componentType = item.ComponentType ?? EditorForm <TModel> .GenerateComponent(fieldType, item.Rows != 0);

                    builder.OpenComponent(0, componentType);
                    builder.AddAttribute(1, "DisplayText", displayName);
                    builder.AddAttribute(2, "Value", fieldValue);
                    builder.AddAttribute(3, "ValueChanged", fieldValueChanged);
                    builder.AddAttribute(4, "ValueExpression", valueExpression);
                    builder.AddAttribute(5, "IsDisabled", item.Readonly);
                    if (IsCheckboxList(fieldType) && item.Data != null)
                    {
                        builder.AddAttribute(6, nameof(CheckboxList <IEnumerable <string> > .Items), item.Data);
                    }
                    builder.AddMultipleAttributes(7, CreateMultipleAttributes(fieldType, fieldName, item));
                    builder.CloseComponent();
                }
            }
        };