public sealed class CompareConverter : CompareConverter<object>
{
}
public abstract class CompareConverter<TObject> : ValueConverterExtension, IValueConverter
{
public IComparable? ComparingValue { get; set; }
public OperatorType ComparisonOperator { get; set; }
public TObject? TrueObject { get; set; }
public TObject? FalseObject { get; set; }
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<x:Double x:Key="ComparingValue">2</x:Double>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Slider x:Name="slider" HorizontalOptions="FillAndExpand" Maximum="4"/>
<Label Text="{Binding Source={x:Reference slider}, Path=Value}"/>
<Label BackgroundColor="{Binding Source={x:Reference slider}, Path=Value,
Converter={xct:CompareConverter ComparisonOperator=Greater,
ComparingValue={StaticResource ComparingValue},
FalseObject=Red, TrueObject=Green}}"
Text="{Binding Source={x:Reference slider}, Path=Value,
Converter={xct:CompareConverter ComparisonOperator=Greater,
ComparingValue={StaticResource ComparingValue}}}"/>
</StackLayout>
</ContentPage>
class MyPage : ContentPage
{
public MyPage()
{
Content = new StackLayout
{
Children =
{
new Slider { Maximum = 4}.FillExpandHorizontal().Assign(out var slider),
new Label().Bind(Label.TextProperty, nameof(Slider.Value), source: slider),
new Label()
.Bind(Label.BackgroundColorProperty, nameof(Slider.Value), source: slider, converter = new CompareConverter { ComparingValue = 2, ComparisonOperator = OperatorType.Greater, FalseObject = Colors.Red, TrueObject = Colors.Green }),
.Bind(Label.TextProperty, named(Slider.Value), source: slider, converter: new CompareConverter { ComparingValue = 2, ComparisonOperator = OperatorType.Greater})
};
}
}