How to pass two inputs to a value converter in xamarin android

霸气de小男生 提交于 2019-12-11 15:56:27

问题


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>

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/45529810/how-to-pass-two-inputs-to-a-value-converter-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!