Jump to: navigation, search

Changes


EvalExpression

264 bytes added, 13:35, 7 January 2021
{{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.}}
==== Evaluation without parameters ====
<tabs>
<tab name="UWP">
 
==== 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.
 
<source lang = "xml">
<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls">
</StackPanel>
</source>
 
The TextBlock should be visible as long as at least one of the context object's two named properties has a value.
</tab>
 
<tab name="Xamarin">
</tab>
</tabs>
==== Simple Calculation ====
<tabs>
<tab name="UWP">
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.
 
<source lang = "xml">
<StackPanel xmlns:ctrls="using:UBIK.WinX.Controls"
</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==== Setting a calculated Property Value ===="Xamarin">{{Attention|Note that extra syntax is required to get the EvalExpression to work in Xamarin!}}
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<source lang = "xml">xmlns:controls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL"...<controls:EvalExpression x:Name="UppercaseTitle" Expression="(P1.ToUpper())" Context="{Binding}"> <controls:EvalExpressionParameter Name="P1" Value="{Binding PropertyItem.MetaProperty.Description}" /></controls:EvalExpression></source>
The above example converts a text (in this situation, the Description text of a property) to a capitalised version. Note that the text <b>Context="{Binding}"</b> is required for the expression to function in Xamarin.
 
To output the result of your expression, use the following syntax:
<source lang = "xml">
<Label Text="{Binding Path=Result, Source={x:Reference UppercaseTitle}}" />
</source>
Note that the correct way to reference elements in Xamarin is x:Reference (ie. the UWP equivalent of ElementName=), followed by the name given to your EvalExpression.
</tab>
</tabs>
 
==== Setting a calculated Property Value ====
<tabs>
<tab name="UWP">
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:
<source lang = "xml">
<Grid xmlns:ctrls="using:UBIK.WinX.Controls">
</Grid>
</source>
</tab>
<tab name== Conditional Statement =="Xamarin"></tab></tabs>
== Conditional Statement ==
If / Or statements can be evaluated using the C# syntax P0 ? P1 : P2.
In this case, if the result of the P0 expression is True, P1 will be effected. Otherwise, P2 will be.
<tabs>
<tab name="UWP">
<source lang = "xml">
<Controls:EvalExpression x:Name="InitialiseParameterWhenNull" Expression="(P0==null) ? P1 : P2">
</Controls:EvalExpression>
</source>
</tab>
<tab name="Xamarin">
</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.
<tabs><tab name="UWP">
</tab>
<tab name="Xamarin">
</tab>
</tabs>
 
EvalExpressions are also available for usage with Xamarin customisations.
{{Attention|Note that extra syntax is required to get the EvalExpression to work in Xamarin!}}
<source lang = "xml">
xmlns:controls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL"
...
<controls:EvalExpression x:Name="UppercaseTitle" Expression="(P1.ToUpper())" Context="{Binding}">
<controls:EvalExpressionParameter Name="P1" Value="{Binding PropertyItem.MetaProperty.Description}" />
</controls:EvalExpression>
</source>
The above example converts a text (in this situation, the Description text of a property) to a capitalised version. Note that the text <b>Context="{Binding}"</b> is required for the expression to function in Xamarin.
 
To output the result of your expression, use the following syntax:
<source lang = "xml">
<Label Text="{Binding Path=Result, Source={x:Reference UppercaseTitle}}" />
</source>
Note that the correct way to reference elements in Xamarin is x:Reference (ie. the UWP equivalent of ElementName=), followed by the name given to your EvalExpression.
 
</tab>
</tabs>