ExpressionPropertyEditor. Parameters

Parameters are named values that are specified in the application and available for use in expressions. Parameters are created in the application code. Values of the parameters can change at different times in the course of execution of the application.

expressionpropertyeditor_7

Parameters are stored in the ParametersDictionary collection. The expression editor ExpressionPropertyEditor exposes two events for access to parameters:

  • CollectParameters: it collects parameter names and descriptions.
  • CollectParametersValues: it supplies values for the parameter names in the course of debugging the formula.

Below is the code snippet demonstrating the use of the parameter collection in the controller.

  • c#
  • VB

public partial class ExpressionEditorParametersViewController : ViewController<DetailView>
{
  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<DynamicParametersObject>().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;
  }
}

Public Partial Class ExpressionEditorParametersViewController
  Inherits ViewController(Of DetailView)
  Public Sub New()
    InitializeComponent()
    RegisterActions(components)
  End Sub
  Protected Overrides Sub OnViewChanged()
    MyBase.OnViewChanged()
    If Me.View Is Nothing OrElse Me.View.ObjectTypeInfo.Type <> GetType(ExpressionObject) Then
      Return
    End If
    Dim propertyEditor = CType(Me.View.Items.FirstOrDefault(Function(ByVal x) x.Id = "Expression"), IExpressionPropertyEditor)
    If propertyEditor Is Nothing Then
      Return
    End If
    propertyEditor.CollectParameters += propertyEditor_CollectParameters
    propertyEditor.CollectParametersValues += PropertyEditorCollectParametersValues
  End Sub
  Private Sub PropertyEditorCollectParametersValues(ByVal sender As Object, ByVal e As CollectParametersValuesEventArgs)
    e.ParametersDictionary("IntValue") = 100
    e.ParametersDictionary("FloatValue") = CType(1.2, Single)
    e.ParametersDictionary("StringValue") = "str"
    e.ParametersDictionary("ObjectValue") = Me.ObjectSpace.GetObjects(Of DynamicParametersObject)().ToList().First()
  End Sub
  Private Sub propertyEditor_CollectParameters(ByVal sender As Object, ByVal e As CollectParametersEventArgs)
    e.ParametersDictionary.Add("IntValue", "IntValue")
    e.ParametersDictionary.Add("FloatValue", "FloatValue")
    e.ParametersDictionary.Add("StringValue", "StringValue")
    e.ParametersDictionary.Add("ObjectValue", "ObjectValue")
  End Sub
  Protected Overrides Sub OnFrameAssigned()
    MyBase.OnFrameAssigned()
    Me.Frame.ViewChanging += FrameViewChanging
  End Sub
  Private Sub FrameViewChanging(ByVal sender As Object, ByVal e As ViewChangingEventArgs)
    If Me.View Is Nothing OrElse Me.View.ObjectTypeInfo.Type <> GetType(ExpressionObject) Then
      Return
    End If
    Dim propertyEditor = CType(Me.View.Items.FirstOrDefault(Function(ByVal x) x.Id = "Expression"), IExpressionPropertyEditor)
    If propertyEditor Is Nothing Then
      Return
    End If
    propertyEditor.CollectParameters -= propertyEditor_CollectParameters
    propertyEditor.CollectParametersValues -= PropertyEditorCollectParametersValues
  End Sub
End Class

You can see the code discussed in this topic in the ExpressionEditorParametersViewController.cs file the Feature Center demo installed with Xafari.

Note:

It is necessary to use the GetValue method of the static ExpressionCalculator class in order to calculate the value of the expression.

The GetValue method has optional parameter Parameters:

  • 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.