How to pass two inputs to a value converter in xam

2019-09-19 07:29发布

What is Happening:

  • I am passing one boolean value to a converter and performing some action(Changing Drawable).

What I am trying to do:

  • How to pass two boolean values to a converter and perform some action.
  • Is this possible How ?
  • If this is not the right approach by passing two inputs to a single value converter, then how to resolve this

CONVERTER : CruiseShipIndicatorValueConverter.cs

public class CruiseShipIndicatorValueConverter : MvxValueConverter<bool, int>
    {
        protected override int Convert(bool value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value)
            {
                return Resource.Drawable.up_arrow;
            }
            else
            {
                return Resource.Drawable.down_arrow;
            }
        }


        protected override bool ConvertBack(int value, Type targetType, object parameter, CultureInfo culture)
        {
            return base.ConvertBack(value, targetType, parameter, culture);
        }

    }

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:gravity="center"
    android:layout_gravity="center"
    android:padding="2dp">
    <MvxImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="2dp"
        android:gravity="center"
        android:layout_gravity="center"
        local:MvxBind="DrawableId QuesSeriesIndicator(questionState)" />
</LinearLayout>

2条回答
Viruses.
2楼-- · 2019-09-19 07:43

You can try to either encapsulate the two booleans to a class or use Tuple.

A sample class implementation for you would be something like this.

public class CruiseShipIndicatorValueConverter : MvxValueConverter<Tuple<bool, bool>, int>

Read more about Tuples here.

查看更多
Emotional °昔
3楼-- · 2019-09-19 07:44

Its very simple. This can be easily done with single value converter. Instead of using tuple or any other generic data type, we can do this by existing converter definition.

For example:

public class CruiseShipIndicatorValueConverter : MvxValueConverter<bool, int>
{

    protected override int Convert(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value)
        {
            return Resource.Drawable.up_arrow;
        }
        else
        {
            return Resource.Drawable.down_arrow;
        }

        if (parameter is bool)
        {
            bool value2 = (bool)parameter;
            // Here this value2 is the second boolean value.
        }
    }

    protected override bool ConvertBack(int value, Type targetType, object parameter, CultureInfo culture)
    {
        return base.ConvertBack(value, targetType, parameter, culture);
    }

}

where MvxValueConverter, bool(questionState1) is the "value" in Convert and int is the return type in Convert. For second bool value(questionState2), get that in "parameter" as type object.

For binding, we have to send

local:MvxBind="DrawableId QuesSeriesIndicator(questionState1, questionState2)" 
查看更多
登录 后发表回答