Extend Functionality
Parameters
A parameter is a named value that is available for use in expressions, it stores in the ParametersDictionary collection. in the process of functioning of the system of values may change. Xafari Expression Property Editor exposes two events to access parameters:
- CollectParameters: it collects parameter names and descriptions.
- CollectParametersValues: maps the parameter name and its value when debugging formulas.
The code snippet below demonstrates how to fill the collection parameters using a controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | public partial class ExpressionEditorParametersViewController : ViewController { public ExpressionEditorParametersViewController() { InitializeComponent(); RegisterActions(components); } protected override void OnViewChanged() { base.OnViewChanged(); if (this.View == null || this.View.ObjectTypeInfo.Type != typeof (ExpressionObject)) return; var propertyEditor = (IExpressionPropertyEditor)this.View.Items.FirstOrDefault(x =>; x.Id == "Expression"); if (propertyEditor == null) return; propertyEditor.CollectParameters += propertyEditor_CollectParameters; propertyEditor.CollectParametersValues += PropertyEditorCollectParametersValues; } private void PropertyEditorCollectParametersValues(object sender, CollectParametersValuesEventArgs e) { e.ParametersDictionary["IntValue"] = 100; e.ParametersDictionary["FloatValue"] = (float)1.2; e.ParametersDictionary["StringValue"] = "str"; e.ParametersDictionary["ObjectValue"] = this.ObjectSpace.GetObjects().ToList().First(); } private void propertyEditor_CollectParameters(object sender, CollectParametersEventArgs e) { e.ParametersDictionary.Add("IntValue", "IntValue"); e.ParametersDictionary.Add("FloatValue", "FloatValue"); e.ParametersDictionary.Add("StringValue", "StringValue"); e.ParametersDictionary.Add("ObjectValue", "ObjectValue"); } protected override void OnFrameAssigned() { base.OnFrameAssigned(); this.Frame.ViewChanging += FrameViewChanging; } private void FrameViewChanging(object sender, ViewChangingEventArgs e) { if (this.View == null || this.View.ObjectTypeInfo.Type != typeof (ExpressionObject)) return; var propertyEditor = (IExpressionPropertyEditor)this.View.Items.FirstOrDefault(x => x.Id == "Expression"); if (propertyEditor == null) return; propertyEditor.CollectParameters -= propertyEditor_CollectParameters; propertyEditor.CollectParametersValues -= PropertyEditorCollectParametersValues; } } |
1 2 | ExpressionCalculator.GetValue(string expression, object context, Dictionary<string, object=""> parameters = null) ExpressionCalculator.GetValue(string expression, Dictionary<string, object=""> parameters = null) |
Use Parameters for passing the values of the parameters.
Add Function
The developer of the business application can implement his own functions to
The following example shows, how to implement and the AddNumbersFunction function, which returns the sum of two numbers passed as parameters.
Implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class AddNumbersFunction : ICustomFunctionOperatorBrowsable { public Type ResultType(params Type[] operands) { return typeof (int); } public object Evaluate(params object[] operands) { return ((int) operands[0]) + ((int) operands[1]); } public string Name { get { return "AddNumbers"; } } public bool IsValidOperandCount(int count) { return count == 2; } public bool IsValidOperandType(int operandIndex, int operandCount, Type type) { if (operandCount != 2) { return false; } return ((operandIndex == 0) || ((operandIndex == 1) && (type == typeof(int)))); } public int MinOperandCount { get { return 2; } } public int MaxOperandCount { get { return 2; } } public string Description { get { return CaptionHelper.GetLocalizedText("Texts", "AddNumberFunction"); } } public FunctionCategory Category { get { return FunctionCategory.All; } } } |
Registration in Module.cs:
1 2 3 4 5 6 7 8 | public override void Setup(XafApplication application) { ... var customFunction = new AddNumbersFunction(); if (CriteriaOperator.GetCustomFunction(customFunction.Name) == null) CriteriaOperator.RegisterCustomFunction(customFunction); ... } |