StateToBooleanConverter
StateToBooleanConverter
è un convertitore unidirezionale che restituisce un boolean
risultato in base al fatto che il valore fornito sia di un oggetto specificoLayoutState
.
Il Convert
metodo restituisce un boolean
risultato in base al fatto che il valore fornito sia di un oggetto specifico LayoutState
. L'enumerazione LayoutState
viene fornita dal toolkit e offre i valori possibili:
None
Loading
Saving
Success
Error
Empty
Custom
Nota
Si noti che il previsto LayoutState
può essere fornito nell'ordine di precedenza seguente:
- come nell'associazione
ConverterParameter
del convertitore; in questo modo laStateToCompare
proprietà viene sostituita StateToCompare
come proprietà nel convertitore
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
Nell'esempio seguente viene illustrato come usare il convertitore per modificare la visibilità di un Label
controllo in base alla LayoutState
proprietà modificata in un oggetto Button
Command
.
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 StateToBooleanConverter
Può StateToBooleanConverter
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.StateToBooleanConverterPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:StateToBooleanConverter x:Key="StateToBooleanConverter" StateToCompare="Success" />
</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout VerticalOptions="Center">
<Label
HorizontalOptions="Center"
IsVisible="{Binding LayoutState, Converter={StaticResource StateToBooleanConverter}}"
Text="The state is Success!"
VerticalOptions="Center" />
<Button Command="{Binding ChangeLayoutCommand}" Text="Change state" />
</VerticalStackLayout>
</ContentPage>
C#
Può StateToBooleanConverter
essere usato come indicato di seguito in C#:
class StateToBooleanConverterPage : ContentPage
{
public StateToBooleanConverterPage()
{
var label = new Label
{
HorizontalOptions = LayoutOptions.Center,
Text = "The state is Success!",
VerticalOptions = LayoutOptions.Center
};
label.SetBinding(
Label.IsVisibleProperty,
new Binding(
static (ViewModel vm) => vm.LayoutState,
converter: new StateToBooleanConverter { StateToCompare = LayoutState.Success }));
var button = new Button
{
Text = "Change state"
};
button.SetBinding(
Button.CommandProperty,
nameof(ViewModel.ChangeLayoutCommand));
Content = new VerticalStackLayout
{
Children =
{
label,
button
}
};
}
}
C# Markup
Il CommunityToolkit.Maui.Markup
pacchetto offre un modo molto più conciso per usare questo convertitore in C#.
using CommunityToolkit.Maui.Markup;
class StateToBooleanConverterPage : ContentPage
{
public StateToBooleanConverterPage()
{
Content = new VerticalStackLayout
{
Children =
{
new Label()
.Text("The state is Success!")
.CenterHorizontal()
.CenterVertical()
.Bind(
Label.IsVisibleProperty,
static (ViewModel vm) => vm.LayoutState,
converter: new StateToBooleanConverter { StateToCompare = LayoutState.Success }),
new Button()
.Text("Change state")
.BindCommand(static (ViewModel vm) => vm.ChangeLayoutCommand)
}
};
}
}
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 StateToBooleanConverter
over nel repository GitHub di .NET MAUI Community Toolkit.
.NET MAUI Community Toolkit