Jump to: navigation, search

Difference between revisions of "EvalExpression"


(Evaluation without parameters)
Line 2: Line 2:
 
| title = {{PAGENAME}}
 
| title = {{PAGENAME}}
 
| name = {{PAGENAME}}
 
| name = {{PAGENAME}}
| namespace= UBIK.WinX.Controls
+
| namespace = "using:UBIK.WinX.Controls" in UBIK.UWP<br>"clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL" in UBIK.Xamarin
 
| image = [[File:SY_{{PAGENAME}}.png|220px]]
 
| image = [[File:SY_{{PAGENAME}}.png|220px]]
 
| imagecaption = imagecaption
 
| imagecaption = imagecaption
 
| purpose = Evaluate a C# expression in XAML
 
| purpose = Evaluate a C# expression in XAML
| version = 3.2+
+
| version = 3.2+ in UBIK.UWP 1.0+ in UBIK.Xamarin
 
}}
 
}}
  
Line 13: Line 13:
  
 
=== Examples ===
 
=== Examples ===
 +
{{Attention|The namespace used in the following examples is for the UBIK.UWP client. If you are applying them to the UBIK.Xamarin clients, please check the table above for the corresponding namespace.}}
  
 
==== Evaluation without parameters ====
 
==== Evaluation without parameters ====
Line 28: Line 29:
  
 
The TextBlock should be visibile as long as at least one of the context object's two named properties has a value.
 
The TextBlock should be visibile as long as at least one of the context object's two named properties has a value.
 
[[Category:Pages with broken file links|EvalExpression]]
 
  
 
==== Simple Calculation ====
 
==== Simple Calculation ====
Line 64: Line 63:
 
</source>
 
</source>
  
[[Category:Pages with broken file links|EvalExpression]]
+
 
  
 
== Parameters ==
 
== Parameters ==

Revision as of 12:33, 9 October 2019

EvalExpression
220px
imagecaption
Name EvalExpression
Namespace "using:UBIK.WinX.Controls" in UBIK.UWP
"clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL" in UBIK.Xamarin
Purpose Evaluate a C# expression in XAML
Version 3.2+ in UBIK.UWP 1.0+ in UBIK.Xamarin

Usage

The EvalExpression control allows to evaluate a C# expression from within XAML markup. The Expression has to be a single-line, valid C# expression ("Lambda") and has to return a single value; expressions can also reference names of subordinate EvalExpressionParameter items.

Examples

IC Attention.pngThe namespace used in the following examples is for the UBIK.UWP client. If you are applying them to the UBIK.Xamarin clients, please check the table above for the corresponding namespace.

Evaluation without parameters

The following example shows how to evaluate a simple expression without using any parameters and then use the result for visibility binding.

<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls">
        <ctrls:EvalExpression
               x:Name="ExpressionEvaluator"
               Context="{Binding Self}"
               Expression="Context.Values[&quot;LK_OFFLINE&quot;]!=null || Context.Values[&quot;GUIDREF&quot;]!=null" />
        <TextBlock Foreground="White" Visibility="{Binding ElementName=ExpressionEvaluator, Path=Result, Converter={StaticResource BoolToVisConverter}}" />
</StackPanel>

The TextBlock should be visibile as long as at least one of the context object's two named properties has a value.

Simple Calculation

The following example shows how to use the control with three parameters, where the first two are user input (Textbox Param0 and Param1) and the third one is a property from its DataContext (ViewModel). The evaluated Result is then bound to a Textblock for output in the UI.

<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls"
        HorizontalAlignment="Center"
        Orientation="Vertical">
        <TextBox x:Name="Expression" Width="200" />
        <TextBox x:Name="Param0" Width="200" />
        <TextBox x:Name="Param1" Width="200" />
        <ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding ElementName=Expression, Path=Text}">
                <ctrls:EvalExpressionParameter Name="P0" Value="{Binding ElementName=Param0, Path=Text, Converter={StaticResource ToType}, ConverterParameter='System.Int32'}" />
                <ctrls:EvalExpressionParameter Name="P1" Value="{Binding ElementName=Param1, Path=Text, Converter={StaticResource ToType}, ConverterParameter='System.Int32'}" />
                <ctrls:EvalExpressionParameter Name="P2" Value="{Binding IsLoggedIn}" />
        </ctrls:EvalExpression>
        <TextBlock Foreground="White" Text="{Binding ElementName=Evaluator, Path=Result}" />
</StackPanel>

Lets assume that the Textbox Param0 contains a text of 42 and Param1 contains a text of 43. If Expression now contains (P0 + P1) *2 then the result would display 170.

Setting a calculated Property Value

The following example shows how to create a button that adds and stores +5 to the value of a a numeric property named MP_EXAMPLE, every time it is pressed:

<Grid xmlns:ctrls="using:UBIK.WinX.Controls">
    <ctrls:EvalExpression x:Name="Evaluator" Expression="&quot;MP_EXAMPLE|&quot; + (P0 + 5)">
        <ctrls:EvalExpressionParameter Name="P0" Value="{Binding Values[MP_EXAMPLE], Converter={StaticResource ToType}, ConverterParameter='System.Int32'}" />
    </ctrls:EvalExpression>
    <Button Content="Tap for 5 more" Command="{Binding SetPropertyValueAndValidateCommand}" CommandParameter="{Binding ElementName=Evaluator, Path=Result}"/>
</Grid>


Parameters

EvalExpressionParameters can be added as child objects to an EvalExpression control. Each parameter object needs a unique Name and a Value, where the latter can be either a constant or dynamic value supplied through a binding.

See also