2014-03-14 57 views
4
的MvxImageView結合本地圖片

我似乎無法讓圖像在MvxListView掙扎與MvvmCross

這裏正確綁定是模板:

<?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="fill_parent" 
    android:layout_height="fill_parent"> 
    <Mvx.MvxImageView 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_margin="10dp" 
     local:MvxBind="ImageUrl IconName, Converter=IconSource" /> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="30dp" 
      local:MvxBind="Text Name" /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="20dp" 
      local:MvxBind="Text Description" /> 
    </LinearLayout> 
</LinearLayout> 

這裏是轉換器:

public class IconSourceValueConverter : MvxValueConverter<string, string> 
{ 
    protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture) 
    { 
     //string retval = string.Format("res:{0}", value.ToLower()); 
     string retval = string.Format("@drawable/{0}", value.ToLower()); 
     return retval; 
    } 
} 

所有圖像出現在Drawable文件夾中。

我試過drawable和res都沒有工作。

我用一個普通的ImageView替換了MvxImageView,它包含一個硬編碼的android:src,它工作正常。

任何想法?

+3

working.Tried我似乎總是把這些東西弄明白我已經發布了一個問題剛過!我錯過了文件和下載緩存插件。轉換器應該使用res:不可繪製。當我被允許時,我會回答我自己的問題。 –

+1

除了'res:'fix之外,您還可以使用自定義綁定目標'DrawableId'作爲int或'DrawableName'而不是'ImageUrl' - 這些都是在https:// github中設置時註冊的。 com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs#L103並使用https://github.com/MvvmCross/MvvmCross/blob/v3.1中的代碼實現/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxImageViewDrawableNameTargetBinding.cs#L38 – Stuart

回答

4

我已經使用這個,使其爲我工作:

public class StringToIntValueConverter : MvxValueConverter<string, int> 
      { 
       protected override int Convert(string value, Type targetType, object parameter, CultureInfo culture) 
       { 
        int image = 0; 
        if(value == "song") 
         image = Resource.Drawable.icon_category_song; 

        return image; 
       } 
      } 

要在Android的佈局中使用這樣的:

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     local:MvxBind="DrawableId StringToInt(Type)" /> 

在這個例子中的「類型」是包含這個詞的字符串「歌曲」。

0

感謝,它與MVXImageview

public class PercentToImageConverter : MvxValueConverter<int, int> 
    {   
    protected override int Convert(int value, Type targetType, object parameter, CultureInfo culture) 

    {  
      switch (value) 
      { 
       case 10: 
        return Resource.Drawable.Percent10; 
       case 40: 
        return Resource.Drawable.Percent40; 
       case 60: 
        return Resource.Drawable.Percent60; 
       case 80: 
        return Resource.Drawable.Percent80; 
       case 100: 
        return Resource.Drawable.Percent100; 
       default: 
        return Resource.Drawable.Percent0;  
      } 

    } 

} 

Android的佈局

<Mvx.MvxImageView 
    android:layout_width="25dp" 
    android:layout_gravity="center" 
    android:layout_height="25dp" 
    local:MvxBind="DrawableId PercentToImage(Percent)" />