VariableMultiValue Converter

De VariableMultiValueConverter is een converter waarmee gebruikers bool-waarden kunnen converteren via een MultiBinding naar één enkele bool. Dit doet u door ze in te schakelen om op te geven of Alle, Any, None of een specifiek aantal waarden waar zijn zoals opgegeven in ConditionType.

De Convert methode retourneert de opgegeven values conversie naar een algemeen bool resultaat op basis van de ConditionType gedefinieerde waarde.

De ConvertBack methode retourneert alleen een resultaat als de ConditionType methode is ingesteld op MultiBindingCondition.All.

Eigenschappen van BaseConverter

De volgende eigenschappen worden geïmplementeerd in de basisklasse: public abstract class BaseConverter

Property Description
DefaultConvertReturnValue Standaardwaarde die wordt geretourneerd wanneer IValueConverter.Convert(object?, Type, object?, CultureInfo?) een Exception genereert. Deze waarde wordt gebruikt wanneer CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters is ingesteld op true.
DefaultConvertBackReturnValue Standaardwaarde die wordt geretourneerd wanneer IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) een Exception genereert. Deze waarde wordt gebruikt wanneer CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters is ingesteld op true.

Eigenschappen van ICommunityToolkitValueConverter

De volgende eigenschappen worden geïmplementeerd in:public interface ICommunityToolkitValueConverter

Property Type Description
DefaultConvertReturnValue object? Standaardwaarde die wordt geretourneerd wanneer IValueConverter.Convert(object?, Type, object?, CultureInfo?) een Exception genereert. Deze waarde wordt gebruikt wanneer CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters is ingesteld op true.
DefaultConvertBackReturnValue object? Standaardwaarde die wordt geretourneerd wanneer IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) een Exception genereert. Deze waarde wordt gebruikt wanneer CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters is ingesteld op true.

Syntax

De volgende voorbeelden laten zien hoe u een Label onzichtbaar maakt wanneer ten minste 2 van de waarden in een MultiBinding als waar worden geëvalueerd.

XAML

Inclusief de XAML-naamruimte

Als u de toolkit in XAML wilt gebruiken, moet de volgende xmlns worden toegevoegd aan uw pagina of weergave:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Daarom volgt het volgende:

<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>

Zou als volgt worden gewijzigd om de xmlns op te nemen:

<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>

VariableMultiValue Converter gebruiken

De VariableMultiValueConverter kan als volgt worden gebruikt 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.VariableMultiValueConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:VariableMultiValueConverter 
                x:Key="VariableMultiValueConverter"
                ConditionType="LessThan"
                Count="2" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <Label Text="At least 2 toppings must be selected.">
        <Label.IsVisible>
            <MultiBinding Converter="{StaticResource VariableMultiValueConverter}">
                <Binding Path="IsCheeseSelected" />
                <Binding Path="IsHamSelected" />
                <Binding Path="IsPineappleSelected" />
            </MultiBinding>
        </Label.IsVisible>
    </Label>

</ContentPage>

C#

De VariableMultiValueConverter kan als volgt worden gebruikt in C#:


class VariableMultiValueConverterPage : ContentPage
{
    public VariableMultiValueConverterPage()
    {
        var label = new Label
        {
            Text = "At least 2 toppings must be selected."
        };

        label.SetBinding(
            Label.IsVisibleProperty,
            new MultiBinding
            {
                Converter = new VariableMultiValueConverter
                {
                    ConditionType = MultiBindingCondition.LessThan,
                    Count = 2
                },
                Bindings = new List<BindingBase>
                {
                    new Binding(static (ViewModel vm) => vm.IsCheeseSelected),
                    new Binding(static (ViewModel vm) => vmIsHamSelected),
                    new Binding(static (ViewModel vm) => vmIsPineappleSelected)
                }
            });

        Content = label;
    }
}

C#-markeringen

Ons CommunityToolkit.Maui.Markup pakket biedt een veel beknoptere manier om dit conversieprogramma in C# te gebruiken.

using CommunityToolkit.Maui.Markup;

class VariableMultiValueConverterPage : ContentPage
{
    public VariableMultiValueConverterPage()
    {
        Content = new Label()
            .Text("At least 2 toppings must be selected.")
            .Bind(
                Label.IsVisibleProperty,
                new List<BindingBase>
                {
                    new Binding(static (ViewModel vm) => vm.IsCheeseSelected),
                    new Binding(static (ViewModel vm) => vm.IsHamSelected),
                    new Binding(static (ViewModel vm) => vm.IsPineappleSelected)
                },
                converter: new VariableMultiValueConverter
                {
                    ConditionType = MultiBindingCondition.LessThan,
                    Count = 2
                });
    }
}

Eigenschappen

Property Type Description
ConditionType MultiBindingCondition Geeft aan hoeveel waarden true moeten zijn van de opgegeven booleaanse waarden in de MultiBinding.
Count int Het aantal waarden dat waar moet zijn bij het gebruik ConditionType van GreaterThan, LessThan of Exact.

MultiBindingCondition

De opsomming MultiBindingCondition definieert de volgende leden:

  • None - Geen van de waarden mag waar zijn.
  • All - Alle waarden moeten waar zijn.
  • Any - Een van de waarden moet waar zijn.
  • Exact - Het exacte getal zoals geconfigureerd in de Count eigenschap moet waar zijn.
  • GreaterThan - Groter dan het getal zoals geconfigureerd in de eigenschap Count moet waar zijn.
  • LessThan - Kleiner dan het getal zoals geconfigureerd in de Count eigenschap moet waar zijn.

Examples

U vindt een voorbeeld van dit conversieprogramma in actie in de .NET MAUI Community Toolkit Sample Application.

API

U vindt de broncode voor VariableMultiValueConverter in de .NET MAUI Community Toolkit GitHub opslagplaats.