IsInRangeConverter
IsInRangeConverter
è un convertitore unidirezionale che accetta un valore in ingresso che implementa IComparable
e restituisce il risultato del valore compreso tra i valori minimo e massimo. Per impostazione predefinita, il risultato sarà un bool
valore se non sono stati specificati oggetti tramite le TrueObject
proprietà e/o FalseObject
. Se i valori vengono assegnati alle TrueObject
proprietà e FalseObject
, IsInRangeConverter restituisce il rispettivo oggetto assegnato.
Nota
Si noti che l'oggetto TrueObject
e FalseObject
deve avere un valore definito o nessuno dei due deve.
Il ConvertBack
metodo non è supportato.
Proprietà BaseConverter
Le proprietà seguenti vengono implementate nella classe base : public abstract class BaseConverter
Proprietà | Descrizione |
---|---|
DefaultConvertReturnValue |
Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un'eccezione Exception . Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true . |
DefaultConvertBackReturnValue |
Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un'eccezione Exception . Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true . |
Proprietà ICommunityToolkitValueConverter
Le proprietà seguenti vengono implementate in public interface ICommunityToolkitValueConverter
:
Proprietà | Type | Descrizione |
---|---|---|
DefaultConvertReturnValue |
object? |
Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un'eccezione Exception . Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true . |
DefaultConvertBackReturnValue |
object? |
Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un'eccezione Exception . Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true . |
Sintassi
XAML
Inclusione dello spazio dei nomi XAML
Per usare il toolkit in XAML, è necessario aggiungere le informazioni seguenti xmlns
nella pagina o nella visualizzazione:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Di conseguenza:
<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>
Verrà modificato in modo da includere l'oggetto xmlns
come indicato di seguito:
<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 di IsInRangeConverter
Può IsInRangeConverter
essere usato come segue in 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#
Può IsInRangeConverter
essere usato come indicato di seguito in 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"
});
}
}
Proprietà
Proprietà | Type | Descrizione |
---|---|---|
MinValue | IComparable |
Valore minimo per l'intervallo di confronto. |
MaxValue | IComparable |
Valore massimo per l'intervallo di confronto. |
FalseObject | object |
Risultato da restituire se il confronto restituisce un false confronto. |
TrueObject | object |
Risultato da restituire se il confronto restituisce un true confronto. |
Esempi
È possibile trovare un esempio di questo convertitore in azione nell'applicazione di esempio .NET MAUI Community Toolkit.
API
È possibile trovare il codice sorgente per IsInRangeConverter
over nel repository GitHub di .NET MAUI Community Toolkit.
.NET MAUI Community Toolkit