Difference between revisions of "EvalExpression"
m |
|||
Line 9: | Line 9: | ||
}} | }} | ||
− | == | + | == 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. | 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. | ||
=== Examples === | === 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 in XAML code, you have to avoid (escape) special characters. There are useful [https://www.freeformatter.com/xml-escape.html online tools] for this.}} | ||
+ | |||
+ | <tabs> | ||
+ | <tab name="UWP"> | ||
==== Evaluation without parameters ==== | ==== Evaluation without parameters ==== | ||
Line 29: | Line 31: | ||
</source> | </source> | ||
− | The TextBlock should be | + | The TextBlock should be visible as long as at least one of the context object's two named properties has a value. |
==== Simple Calculation ==== | ==== Simple Calculation ==== | ||
Line 53: | Line 55: | ||
==== Setting a calculated Property Value ==== | ==== 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: | 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: | ||
Line 65: | Line 68: | ||
== Conditional Statement == | == Conditional Statement == | ||
+ | |||
If / Or statements can be evaluated using the C# syntax P0 ? P1 : P2. | 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. | In this case, if the result of the P0 expression is True, P1 will be effected. Otherwise, P2 will be. | ||
Line 80: | Line 84: | ||
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. | 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. | ||
− | = | + | </tab> |
+ | |||
+ | <tab name="Xamarin"> | ||
EvalExpressions are also available for usage with Xamarin customisations. | EvalExpressions are also available for usage with Xamarin customisations. | ||
Line 101: | Line 107: | ||
</source> | </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. | 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> | ||
==See also== | ==See also== |
Revision as of 12:46, 7 January 2021
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 |
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 EvalExpressionParameter items.
Examples
When writing expressions in XAML code, you have to avoid (escape) special characters. There are useful online tools for this. |
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.
<ctrls:EvalExpression
x:Name="ExpressionEvaluator"
Context="{Binding Self}"
Expression="Context.Values["LK_OFFLINE"]!=null || Context.Values["GUIDREF"]!=null" />
<TextBlock Foreground="White" Visibility="{Binding ElementName=ExpressionEvaluator, Path=Result, Converter={StaticResource BoolToVisConverter}}" />
</StackPanel>
The TextBlock should be visible 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.
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:
<ctrls:EvalExpression x:Name="Evaluator" Expression=""MP_EXAMPLE|" + (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>
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.
<Controls:EvalExpressionParameter Name="P0" Value="{Binding StoredProfileParameters[WPType1]}" />
<Controls:EvalExpressionParameter Name="P1" Value="WPType1=1" />
<Controls:EvalExpressionParameter Name="P2" Value="{Binding ElementName=InitialiseParameter, Path=Result}" />
</Controls:EvalExpression>
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.
Xamarin
EvalExpressions are also available for usage with Xamarin customisations.
...
<controls:EvalExpression x:Name="UppercaseTitle" Expression="(P1.ToUpper())" Context="{Binding}">
<controls:EvalExpressionParameter Name="P1" Value="{Binding PropertyItem.MetaProperty.Description}" />
</controls:EvalExpression>
The above example converts a text (in this situation, the Description text of a property) to a capitalised version. Note that the text Context="{Binding}" is required for the expression to function in Xamarin.
To output the result of your expression, use the following syntax:
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.