FunctionZero
  • Libraries
    • Maui.MvvmZero
      • Overview
      • Quickstart
      • Walkthrough
        • Create your application
        • Create your ContentPages
        • Create your ViewModels
        • Register your new content
        • Launching our app!
        • Adding Navigation
        • Summary
      • Recommended naming conventions
      • Configuring your root Page
        • Root ContentPage
        • Root NavigationPage
        • Root TabbedPage
        • Root FlyoutPage
      • A note on Singleton vs Transient
      • Recommended base classes for PageViewModels and ViewModels
    • Maui.Controls
      • ExpanderZero
      • FocusScrollZero
      • LabelZero
      • ListViewZero
      • MaskZero
      • MultiViewZero
      • TreeViewZero
    • CommandZero
    • Maui.BindZero
      • Quickstart
      • z:Bind
      • Examples
      • The Great Escape
      • Casting
      • Short Circuit
      • Errors
      • Aliases
      • Value types and Reference types
      • z:Function
      • z:TapTrigger
      • z:EdgeTrigger
      • z:Latch
      • Advanced Usage - Functions, aliases and operator-overloads
    • AnimationZero
    • LocalisationZero
Powered by GitBook
On this page
  1. Libraries
  2. Maui.BindZero

Advanced Usage - Functions, aliases and operator-overloads

Previousz:LatchNextAnimationZero

Last updated 1 year ago

z:Bind uses to do the heavy lifting, so take a look at the if you want to take a deeper dive. Here is a taster ...

Functions

Sin, Cos and Tan are registered by default, as are the aliases listed above.

<Label TranslationX="{z:Bind Sin(Count / 25.0) * 100.0}" ...

Suppose you wanted a new function to to do a linear interpolation between two values, like this: (Spoiler: Lerp is also pre-registered)

float Lerp(float a, float b, float t)
{
  return a + t * (b - a);
}

For use like this:

<Label Rotation={z:Bind Lerp(0, 360, rotationPercent / 100.0)} ...

First you will need a reference to the default ExpressionParser

var ep = ExpressionParserFactory.GetExpressionParser();

Then register a function that takes 3 parameters:

ep.RegisterFunction("Lerp", DoLerp, 3);

Finally write the DoLerp method referenced above.

private static void DoLerp(Stack<IOperand> stack, IBackingStore backingStore, long paramCount)
{
    // Pop the correct number of parameters from the operands stack, ** in reverse order **
    // If an operand is a variable, it is resolved from the backing store provided
    IOperand third = OperatorActions.PopAndResolve(operands, backingStore);
    IOperand second = OperatorActions.PopAndResolve(operands, backingStore);
    IOperand first = OperatorActions.PopAndResolve(operands, backingStore);

    float a = Convert.ToSingle(first.GetValue());
    float b = Convert.ToSingle(second.GetValue());
    float t = Convert.ToSingle(third.GetValue());

    // The result is of type float
    float result = a + t * (b - a);

    // Push the result back onto the operand stack
    stack.Push(new Operand(-1, OperandType.Float, result));
}

Aliases

Get a reference to the default ExpressionParser:

var ep = ExpressionParserFactory.GetExpressionParser();

Then register a new operator and use the existing matrix for &&

ep.RegisterOperator("AND", 4, LogicalAndMatrix.Create());

Overloads

Suppose you want to add a long to a string

Get a reference to the default ExpressionParser:

var ep = ExpressionParserFactory.GetExpressionParser();

Then simply register the overload like this

// Overload that will allow a long to be appended to a string
// To add a string to a long you'll need to add another overload
ep.RegisterOverload("+", OperandType.String, OperandType.Long, 
    (left, right) => new Operand(OperandType.String, (string)left.GetValue() + ((long)right.GetValue()).ToString()));

and to add a string to a long:

// Overload that will allow a string to be appended to a long
// To add a long to a string you'll need to add another overload
ep.RegisterOverload("+", OperandType.Long, OperandType.String, 
    (left, right) => new Operand(OperandType.String, (long)left.GetValue() + ((string)right.GetValue()).ToString()));

Putting the above into action, you can then start to really have some fun

<Label 
    Text="{z:Bind '\'Player 1 score \' + playerOne.Score + \'points\''}
    Rotation="{z:Bind 'Lerp(0, 360, rotationPercent / 100.0)'"}
/>

(See the ExpressionParserZero for more details)

FunctionZero.ExpressionParserZero
documentation
source and documentation