Changes
#2161 - Added XAML Category
}}
== Concept ==
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 [[EvalExpression#Parameters|EvalExpressionParameter]] items.
== Concept= Parameters ===The EvalExpressionParameters can be added as child objects to an EvalExpression control allows to evaluate a C# expression from within XAML markup. The Each parameter object needs a unique ''ExpressionName'' has to be and a single-line''Value'', valid C# expression ("Lambda") and has to return where the latter can be either a single constant or dynamic value; expressions can also reference names of subordinate [[EvalExpression#Parameters|EvalExpressionParameter]] itemssupplied through a binding.
=== Examples ===
{{Hint|When writing expressions in XAML code, you have to avoid (escape) special characters. There are useful [https://www.freeformatter.com/xml-escape.html online tools] for this.}}
{{Hint|When writing expressions, it's always better (even necessary in some cases) to write them in their full forms. For example, while <nowiki>Bool_A || Bool_B</nowiki> is a valid expression, you should still write <nowiki>Bool_A==true || Bool_B==true</nowiki> instead.}}
{{Hint|If you want to use bindings in EvalExpressionParameters, a lot of the times you need to add <nowiki>Context="{Binding}"</nowiki> to the EvalExpression. This is because child UI elements (parameters in this case) do not inherit the binding context of the parent by default. Manually setting the context that way ensures that the same binding expressions that work outside the EvalExpression also work inside. Although this is not necessary if the binding you use explicitly refers to a named UI element.}}
==== 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.
<tabs>
<tab name="UWP">
<source lang = "xml">
<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls">
</StackPanel>
</source>
<tab name="Xamarin">
<source lang = "xml">
<StackLayout xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
</StackLayout>
</source>
The Label should be visible as long as at least one of the context object's two named properties has a value.</tab>
</tabs>
==== Simple Calculation calculation ====
<tabs>
<tab name="UWP">
The following example shows how to use the control an expression with three two 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 TextBlock for output in the UI.
<source lang = "xml">
<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}" Context="{Binding}"> <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>
</source>
Lets assume that the Textbox ''Param0'' contains a text of 42 and ''Param1'' contains a text of 43. If ''Expression'' now contains <code>(P0 + P1) *2</code> then the result would display 170.
</tab>
<tab name="Xamarin -> example under construction">The following example shows how to use the control an expression with three parameters, where the first two are user input parameters (Editor ''Param0'' and ''Param1'') and the third one is a property from its DataContext (ViewModel). The evaluated ''Result'' is then bound to an a Label for output in the UI.
<source lang = "xml">
<StackLayout xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
</StackLayout>
</source>
</tabs>
<tabs>
<tab name="UWP">
<source lang = "xml">
<Grid xmlns:ctrls="using:UBIK.WinX.Controls">
<ctrls:EvalExpression x:Name="Evaluator" Expression=""MP_EXAMPLE|" + (P0 + 5)" Context="{Binding}"> <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>
</source>
<tab name="Xamarin">
<source lang = "xml">
<Grid xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
</Grid>
</source>
</tabs>
==== Conditional statement ====
If / Or statements can also be evaluated using C# syntax.
In this case, if the result of the "Condi" parameter is true, namely if the context object's MP_STATUS property value equals to 0, P0's value will be displayed in the Label, otherwise P1's value will be displayed.
<tabs>
<tab name="UWP">
<source lang = "xml">
</source>
</tab>
<tab name="Xamarin">
<source lang = "xml">
<Grid xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
</source>
</tab>
==See also==
[[Category:WinX|EvalExpression]]
[[Category:Xamarin|EvalExpression]]
[[Category:XAML|EvalExpression]]