Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to write a custom IMarkupExtension that can calculate reference values?

$
0
0

I've got a control on screen that needs a relative layout that has a Constant that needs to be calculated.

So My XConstraint looks like this.

RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=ReferenceLegend, Property=Width, Constant={localization:CalculateValue (ReferenceLegend.Width - 40)}}"

and I've started wiring up an IMarkupExtension

[ContentProperty("Input")]
public class CalculateValueExtension : IMarkupExtension
{
    public string Input { get; set; }
    public object ProvideValue(IServiceProvider serviceProvider)
    {

        var expression = Input;  // "(ReferenceLegend.Width - 40)"

        return // calculation.
    }
}

But what I need to know is how I might dig into the serviceProvider in order to locate the ReferenceLegend.Width, and then convert the string (ReferenceLegend.Width - 40) into something I can evaluate.

Side note: Am I over complicating this? Is there something already wired in?


Viewing all articles
Browse latest Browse all 89864

Trending Articles