次の方法で共有


MathExpressionConverter

MathExpressionConverterは、ユーザーがさまざまな算術演算を実行できるようにするコンバーターです。 これは単一の Binding 値で動作します。 MultiBinding を使用して複数の値が必要な場合は、 MultiMathExpressionConverter

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

コンバーターに渡される値には、 xという名前が付けられます。 式内でこの値を参照するには、 x を使用する必要があります (たとえば、 x / 2 は受信値を 2 で除算します)。 式内のその他の変数名は無視されます。

BaseConverter プロパティ

基底クラスでは、次のプロパティが実装 public abstract class BaseConverter

プロパティ 説明
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

ICommunityToolkitValueConverter プロパティ

public interface ICommunityToolkitValueConverterでは、次のプロパティが実装されています。

プロパティ タイプ 説明
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue object? IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

構文

以下の例は、xMyValueの値を与えたときのx / 2の結果を表示するLabelを追加する方法を示します。

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>

MathExpressionConverter の使用

MathExpressionConverterは、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.MathExpressionConverterPage">

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

    <Label Text="{Binding MyValue, Converter={StaticResource MathExpressionConverter}, ConverterParameter='x/2'}" />

</ContentPage>

C#

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

class MathExpressionConverterPage : ContentPage
{
    public MathExpressionConverterPage()
    {
        var label = new Label();

        label.SetBinding(
            Label.TextProperty,
            new Binding(
                static (ViewModels vm) => vm.MyValue,
                converter: new MathExpressionConverter(),
                converterParameter: "x/2"));

        Content = label;
    }
}

C# Markup

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

using CommunityToolkit.Maui.Markup;

class MathExpressionConverterPage : ContentPage
{
    public MathExpressionConverterPage()
    {
        Content = new Label()
            .Bind(
                Label.TextProperty,
                static (ViewModel vm) => vm.MyValue,
                converter: new MathExpressionConverter(),
                converterParameter: "x/2");
    }
}

サポート対象の操作

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

  • "+"
  • "-"
  • "*"
  • "/"
  • "%"
  • "?"":"
  • "=="
  • "!="
  • "!"
  • "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 (アプリケーション・プログラミング・インターフェース)

MathExpressionConverter のソース コードは、.NET MAUI Community Toolkit の GitHub リポジトリにあります。