2017-08-06 32 views
0

發生什麼事如何通過兩個輸入值轉換器在xamarin安卓

  • 我傳遞一個布爾值轉換器和執行一些動作 (變更可繪製)。

我所試圖做

  • 如何兩個布爾值傳遞給一個轉換器,並執行一些動作 。
  • 這是可能的如何?
  • 如果不是通過將兩個輸入到一個 值轉換器正確的方法,那麼如何解決這個

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

你要不要MultiValueConverter之爲知之?看看https://github.com/Keboo/Xamarin.Forms.Proxy –

回答

0

您可以嘗試將兩個布爾值封裝到一個類中或使用Tuple。

您的示例類實現將是這樣的。

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

瞭解更多關於Tuples here

+0

謝謝, 如何將值傳遞給XML轉換器 **我可以試試這個:**'local:MvxBind =「DrawableId QuesSeriesIndicator(questionState1,questionState2)「' ....其中... 'questionState1'和'questionState2'是兩個布爾輸入 – Devrath

+0

@Devrath,你找到了解決方案嗎? – androidStud

0

它非常簡單。這可以通過單個值轉換器輕鬆完成。 除了使用元組或其他任何通用數據類型,我們可以通過現有的轉換器定義來實現。

例如:

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,布爾(questionState1)是轉換的 「價值」 和int是在轉換的返回類型。對於第二個bool值(questionState2),將其作爲類型對象的「參數」。

進行結合,我們必須通過WPF發送

local:MvxBind="DrawableId QuesSeriesIndicator(questionState1, questionState2)"