Changes

EvalExpression

233 bytes removed, 11:04, 26 January 2021
== 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.
=== 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.
=== 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.}}
==== Evaluation without parameters ====
<source lang = "xml">
<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls">
<ctrls:EvalExpression x:Name="ExpressionEvaluatorEvaluator" Context="{Binding Self}" Expression="Context.Values[&quot;LK_OFFLINE&quot;]!=null || Context.Values[&quot;GUIDREF&quot;]!=null" /> <TextBlock Foreground="White" Visibility="{Binding ElementName=ExpressionEvaluatorEvaluator, Path=Result, Converter={StaticResource BoolToVisConverter}}" />
</StackPanel>
</source>
<source lang = "xml">
<StackLayout xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
<ctrls:EvalExpression x:Name="ControllingVisibilityEvaluator" Context="{Binding}" Expression="Context.Values[&quot;LK_EXAMPLE&quot;]!=null || Context.Values[&quot;GUIDREF&quot;]!=null" /> <Label TextColor="#00000" IsVisible="{Binding Path=Result, Source={x:Reference ControllingVisibilityEvaluator}, Converter={StaticResource NullToBoolConverter}}" />
</StackLayout>
</source>
</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">
The following example shows how to use the control an expression with three two parameters, where the first two are user input (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">
<Editor x:Name="Expression"/> <Editor x:Name="P0" WidthRequest="32"/> <Editor x:Name="P1" WidthRequest="43"/> <ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding Path=Text, Source={x:Reference Expression}}" Context="{Binding}"> <ctrls:EvalExpressionParameter Name="P0" Value="{Binding Path=WidthRequest, Source={x:Reference P0}}" /> <ctrls:EvalExpressionParameter Name="P1" Value="{Binding Path=WidthRequest, Source={x:Reference P1}}" /> </ctrls:EvalExpression> <Label Text="{Binding Path=Result, Source={x:Reference Evaluator}}" />
</StackLayout>
</source>
Lets assume that the Since Textbox ''P0'' has a width of 32 and ''P1'' has a width of 43. If ''Expression'' now contains <code>(P0 + P1)</code> then the result would display 75.</tab>
</tabs>
[[Category:Client|EvalExpression]][[Category:Pages with broken file links|EvalExpression]][[Category:WinX|EvalExpression]][[Category:Xamarin|EvalExpression]] ==== Setting a calculated Property Value property value ====
<tabs>
<tab name="UWP">
<source lang = "xml">
<Grid xmlns:ctrls="using:UBIK.WinX.Controls">
<ctrls:EvalExpression x:Name="Evaluator" Expression="&quot;MP_EXAMPLE|&quot; + (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>
<source lang = "xml">
<Grid xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
<ctrls:EvalExpression x:Name="Evaluator2Evaluator" Expression="&quot;MP_EXAMPLE|&quot; + (P0 + 2)" Context="{Binding}"> <ctrls:EvalExpressionParameter Name="P0" Value="{Binding Values[MP_EXAMPLE]}" /> </ctrls:EvalExpression> <Button Text="Tap to add 2 more" Command="{Binding SetPropertyValueAndValidateCommand}" CommandParameter="{Binding Path=Result, Source={x:Reference Evaluator2Evaluator}}"/>
</Grid>
</source>
</tabs>
     ==== Conditional Statement statement ====
<tabs>
<tab name="UWP">
In this case, if the result of the P0 expression is True, P1 will be effected. Otherwise, P2 will be.
<source lang = "xml">
<Grid xmlns:ctrls="using:UBIK.WinX.Controls"> <ctrls:EvalExpression x:Name="InitialiseParameterWhenNull" Expression="(P0==null) ? P1 : P2" Context="{Binding}"> <Controlsctrls:EvalExpressionParameter Name="P0" Value="{Binding StoredProfileParameters[WPType1]}" /> <Controlsctrls:EvalExpressionParameter Name="P1" Value="WPType1=1" /> <Controlsctrls:EvalExpressionParameter Name="P2" Value="{Binding ElementName=InitialiseParameter, Path=Result}" /> </Controlsctrls:EvalExpression></Grid>
</source>
</tab>
<source lang = "xml">
<Grid xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL">
<controls:EvalExpression x:Name="StatusAutomatorEvaluator" Expression="P0 == true ? P00 : (P1 == true ? P10)" Context="{Binding}"> <controls:EvalExpressionParameter Name="P0" Value="{Binding Values[MP_STATUS], Converter={StaticResource EqualityToBool}, ConverterParameter=0}" /> <controls:EvalExpressionParameter Name="P00" Value="State1" /> <controls:EvalExpressionParameter Name="P1" Value="{Binding Values[MP_STATUS], Converter={StaticResource EqualityToBool}, ConverterParameter=10}" /> <controls:EvalExpressionParameter Name="P10" Value="State2" /> </controls:EvalExpression> <Label Text="{Binding Path=Result, Source={x:Reference StatusAutomatorEvaluator}}" FontSize="16"/> </Grid>
</source>
</tab>
</tabs>
 
 
 
 
 
 
== 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==
[[Category:Client|EvalExpression]]
[[Category:Pages with broken file links|EvalExpression]]
[[Category:WinX|EvalExpression]]
[[Category:Xamarin|EvalExpression]]