IsInRangeConverter
IsInRangeConverter
es un convertidor unidireccional que toma un valor entrante que implementa IComparable
, y un valor mínimo y máximo, y devuelve el resultado del valor que está entre los valores mínimo y máximo. El resultado tendrá como valor predeterminado bool
si no se especificó ningún objeto mediante las propiedades TrueObject
o FalseObject
. Si los valores se asignan a las propiedades TrueObject
y FalseObject
, IsInRangeConverter devuelve el objeto correspondiente asignado.
Nota:
Tenga en cuenta que ambos, TrueObject
y FalseObject
, deben tener un valor definido o ninguno de ellos.
No se admite el método ConvertBack
.
Propiedades de BaseConverter
Las siguientes propiedades se implementan en la clase base, public abstract class BaseConverter
:
Propiedad | Descripción |
---|---|
DefaultConvertReturnValue |
Valor predeterminado que se devuelve cuando IValueConverter.Convert(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
DefaultConvertBackReturnValue |
Valor predeterminado que se devuelve cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
Propiedades de ICommunityToolkitValueConverter
Las siguientes propiedades se implementan en public interface ICommunityToolkitValueConverter
:
Propiedad | Tipo | Descripción |
---|---|---|
DefaultConvertReturnValue |
object? |
Valor predeterminado que se devuelve cuando IValueConverter.Convert(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
DefaultConvertBackReturnValue |
object? |
Valor predeterminado que se devuelve cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
Sintaxis
XAML
Incluir el espacio de nombres XAML
Para usar el kit de herramientas en XAML, es necesario agregar el siguiente xmlns
a la página o vista:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Por lo tanto, el siguiente:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
Se modificaría para incluir el xmlns
de la siguiente manera:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
Uso de IsInRangeConverter
El IsInRangeConverter
se puede usar de la siguiente manera en XAML:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.IsInRangeConverterPage">
<ResourceDictionary>
<x:String x:Key="MinValue">Hello</x:String>
<x:String x:Key="MaxValue">World</x:String>
<Color x:Key="TrueColor">Green</Color>
<Color x:Key="FalseColor">Red</Color>
</ResourceDictionary>
<Label BackgroundColor="{Binding MyValue, Converter={toolkit:IsInRangeConverter TrueObject={StaticResource TrueColor}, FalseObject={StaticResource FalseColor}, MinValue={StaticResource MinValue}, MaxValue={StaticResource MaxValue}}}" Text="The background of this label will be green if MyValue is between 'Hello' and 'World', otherwise red." />
</ContentPage>
C#
El IsInRangeConverter
se puede usar de la siguiente manera en C#:
class IsInRangeConverterPage : ContentPage
{
public IsInRangeConverterPage()
{
Content = new Label()
.Text("The background of this label will be green if MyValue is between 'Hello' and 'World', otherwise red.")
.Bind(
Label.BackgroundColorProperty,
static (ViewModel vm) => vm.MyValue,
converter: new IsInRangeConverter
{
TrueObject = Colors.Green,
FalseObject = Colors.Red,
MinValue = "Hello",
MaxValue = "World"
});
}
}
Propiedades
Propiedad | Tipo | Descripción |
---|---|---|
MinValue | IComparable |
Valor mínimo del intervalo de comparación. |
MaxValue | IComparable |
Valor máximo del intervalo de comparación. |
FalseObject | object |
Resultado que se va a devolver si la comparación da como resultado una comparación false . |
TrueObject | object |
Resultado que se va a devolver si la comparación da como resultado una comparación true . |
Ejemplos
Puede encontrar un ejemplo de este convertidor en acción en la Aplicación de muestra del kit de herramientas de la comunidad de .NET MAUI.
API
Puede encontrar el código fuente de IsInRangeConverter
en el repositorio de GitHub del Kit de herramientas de la comunidad de .NET MAUI.
.NET MAUI Community Toolkit