Difference between revisions of "EvalExpression"
(→Simple Calculation) |
(→Simple Calculation) |
||
Line 74: | Line 74: | ||
<tab name="Xamarin"> | <tab name="Xamarin"> | ||
+ | HJELP - this doesn't work | ||
The following example shows how to use the control with three 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 Label for output in the UI. | The following example shows how to use the control with three 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 Label for output in the UI. | ||
<source lang = "xml"> | <source lang = "xml"> | ||
<StackLayout xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL"> | <StackLayout xmlns:ctrls="clr-namespace:UBIK.CPL.Controls;assembly=UBIK.CPL"> | ||
<Editor x:Name="Expression" Width="200" /> | <Editor x:Name="Expression" Width="200" /> | ||
− | <Editor | + | <Editor x:Name="Param0" Width="32"/> |
− | <Editor x:Name="Param1" Width=" | + | <Editor x:Name="Param1" Width="43"/> |
− | <ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding | + | <ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding Path=Text, Source={x:Reference Expression}}"> |
− | + | <ctrls:EvalExpressionParameter Name="P0" Value="{Binding Path=Width, Source={x:Reference Param0}}" /> | |
− | + | <ctrls:EvalExpressionParameter Name="P1" Value="{Binding Path=Width, Source={x:Reference Param1}}" /> | |
− | + | </ctrls:EvalExpression> | |
− | + | <Label Text="{Binding Path=Result, Source={x:Reference Evaluator}}" /> | |
− | <Label Text="{Binding | + | |
</StackLayout> | </StackLayout> | ||
− | + | Lets assume that the Editor ''Param0'' has a width of 32 and ''Param1'' has a width of 43. If ''Expression'' now contains <code>(P0 + P1) *2</code> then the result would display 170.</tab> | |
− | Lets assume that the | + | |
</tabs> | </tabs> | ||
Revision as of 11:38, 25 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 |
Contents
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. |
Evaluation without parameters
UWP
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.
Xamarin
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="ControllingVisibility"
Context="{Binding}"
Expression="Context.Values["LK_EXAMPLE"]!=null || Context.Values["GUIDREF"]!=null" />
<Label
TextColor="#00000"
IsVisible="{Binding Path=Result, Source={x:Reference ControllingVisibility}, Converter={StaticResource NullToBoolConverter}}" />
</StackLayout>
Simple Calculation
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.
<Editor x:Name="Expression" Width="200" />
<Editor x:Name="Param0" Width="32"/>
<Editor x:Name="Param1" Width="43"/>
<ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding Path=Text, Source={x:Reference Expression}}">
<ctrls:EvalExpressionParameter Name="P0" Value="{Binding Path=Width, Source={x:Reference Param0}}" />
<ctrls:EvalExpressionParameter Name="P1" Value="{Binding Path=Width, Source={x:Reference Param1}}" />
</ctrls:EvalExpression>
<Label Text="{Binding Path=Result, Source={x:Reference Evaluator}}" />
</StackLayout>
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.
Xamarin
HJELP - this doesn't work The following example shows how to use the control with three 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 Label for output in the UI.
<Editor x:Name="Expression" Width="200" />
<Editor x:Name="Param0" Width="32"/>
<Editor x:Name="Param1" Width="43"/>
<ctrls:EvalExpression x:Name="Evaluator" Expression="{Binding Path=Text, Source={x:Reference Expression}}">
<ctrls:EvalExpressionParameter Name="P0" Value="{Binding Path=Width, Source={x:Reference Param0}}" />
<ctrls:EvalExpressionParameter Name="P1" Value="{Binding Path=Width, Source={x:Reference Param1}}" />
</ctrls:EvalExpression>
<Label Text="{Binding Path=Result, Source={x:Reference Evaluator}}" />
</StackLayout>
Lets assume that the Editor ''Param0'' has a width of 32 and ''Param1'' has a width of 43. If ''Expression'' now contains <code>(P0 + P1) *2</code> then the result would display 170.
Setting a calculated Property Value
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:
<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>
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:
<ctrls:EvalExpression x:Name="Evaluator2" Expression=""MP_EXAMPLE|" + (P0 + 2)">
<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 Evaluator2}}"/>
</Grid>
Conditional Statement
UWP
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>
Xamarin
If / Or statements can also be evaluated using various C# syntax. In this case, if the result of the P0 expression is true P00 will be used, if P1 is true P10 will be used.
<controls:EvalExpression x:Name="StatusAutomator" 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 StatusAutomator}}"
FontSize="16"/>
</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.