如何在Xamarin Android中将两个输入传递给值转换器 [英] How to pass two inputs to a value converter in xamarin android

查看:66
本文介绍了如何在Xamarin Android中将两个输入传递给值转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在发生什么 :

What is Happening:

  • 我正在将一个布尔值传递给转换器并执行一些 动作(更改Drawable).
  • 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

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

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

推荐答案

非常简单.使用单值转换器可以轻松完成此操作. 可以使用现有的转换器定义来执行此操作,而不必使用元组或任何其他常规数据类型.

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.

例如:

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);
    }

}

其中MvxValueConverter,bool(questionState1)是Convert中的值",而int是Convert中的返回类型.对于第二个布尔值(questionState2),请在参数"中将其作为对象类型.

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

这篇关于如何在Xamarin Android中将两个输入传递给值转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆