MultiMathExpressionConverter

MultiMathExpressionConverterは、ユーザーがMultiBindingを使用して複数の値を持つさまざまな算術演算を実行できるようにするコンバーターです。

Convertは、複数の変数を持つConverterParameterで定義された式文字列を計算し、double結果を返します。

コンバーターに渡される値は、 x? の場所に名前が付けられます。 は、 MultiBindingで定義されている順序です。式内の他の変数名は無視されます。 例えば、 P = V * I の計算(パワー=ボルト*アンプ)を表現するには、次のように記述することができます。

<Label.Text>
    <MultiBinding Converter="{StaticResource MultiMathExpressionConverter}" ConverterParameter="x0 * x1">
        <Binding Path="Volts" />
        <Binding Path="Amps" />
    </MultiBinding>
</Label.Text>

Syntax

次の例では、x0 + x1 + x2 の結果を表示する Label を追加する方法を示します。x の値は MultiBinding の定義の順序で指定されます。

XAML

XAML 名前空間を含める

XAML でこのツールキットを使用するには、次の xmlns をページまたはビューに追加する必要があります。

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

したがって、以下の通りです

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

次のように、xmlns を含むように変更されます。

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

MultiMathExpressionConverter の使用

MultiMathExpressionConverterは、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.MultiMathExpressionConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:MultiMathExpressionConverter x:Key="MultiMathExpressionConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <Label HorizontalOptions="Center">
        <Label.Text>
            <MultiBinding Converter="{StaticResource MultiMathExpressionConverter}" ConverterParameter="x0 + x1 + x2">
                <Binding Path="X0" />
                <Binding Path="X1" />
                <Binding Path="X2" />
            </MultiBinding>
        </Label.Text>
    </Label>

</ContentPage>

C#

MultiMathExpressionConverterは、C# で次のように使用できます。

class MultiMathExpressionConverterPage : ContentPage
{
    public MultiMathExpressionConverterPage()
    {
        var label = new Label
        {
            HorizontalOptions = LayoutOptions.Center
        };

        label.SetBinding(
            Label.TextProperty,
            new MultiBinding
            {
                Converter = new MultiMathExpressionConverter(),
                ConverterParameter = "x0 + x1 + x2",
                Bindings = new List<BindingBase>
                {
                    new Binding(static (ViewModel vm) => vm.X0),
                    new Binding(static (ViewModel vm) => vm.X1),
                    new Binding(static (ViewModel vm) => vm.X2)
                }
            });

        Content = label;
    }
}

C# マークアップ

この CommunityToolkit.Maui.Markup パッケージは、C# でこのコンバーターを使用するためのより簡潔な方法を提供します。

class MultiMathExpressionConverterPage : ContentPage
{
    public MultiMathExpressionConverterPage()
    {
        Content = new Label()
            .CenterHorizontal()
            .Bind(
                Label.TextProperty,
                new List<BindingBase>
                {
                    new Binding(static (ViewModel vm) => vmX0),
                    new Binding(static (ViewModel vm) => vmX1),
                    new Binding(static (ViewModel vm) => vmX2)
                },
                converter: new MultiMathExpressionConverter(),
                converterParameter: "x0 + x1 + x2");
    }
}

サポートされている操作

次の操作がサポートされています。

  • "+"
  • "-"
  • "*"
  • "/"
  • "%"
  • "?" ":"
  • "=="
  • "!="
  • "!"
  • "or" または "||"
  • "and" または "&&" XAML でこれを使用する場合は、文字をエスケープする必要があります (例: "&amp;&amp;")
  • "ge" または ">=" XAML でこれを使用する場合は、文字をエスケープする必要があります (例: "&gt;=")
  • "gt" または ">" XAML でこれを使用する場合は、文字をエスケープする必要があります (例: "&gt;")
  • "le" または "<=" XAML でこれを使用する場合は、文字をエスケープする必要があります (例: "&lt;=")
  • "lt" または "<" XAML でこれを使用する場合は、文字をエスケープする必要があります (例: "&lt;")
  • "abs"
  • "acos"
  • "asin"
  • "atan"
  • "atan2"
  • "ceiling"
  • "cos"
  • "cosh"
  • "exp"
  • "floor"
  • "ieeeremainder"
  • "log"
  • "log10"
  • "max"
  • "min"
  • "pow"
  • "round"
  • "sign"
  • "sin"
  • "sinh"
  • "sqrt"
  • "tan"
  • "tanh"
  • "truncate"
  • "^"
  • "pi"
  • "e"
  • "true"
  • "false"

例示

このコンバーターの動作例は、.NET MAUI Community Toolkit サンプル アプリケーションで確認できます。

API

MultiMathExpressionConverter のソース コードは、.NET MAUI Community Toolkit GitHub リポジトリで確認できます。