努力将MvxImageView中的本地图像与MvvmCross绑定

时间:2014-03-14 03:01:07

标签: android mvvmcross

我似乎无法在MvxListView中正确绑定图像

以下是模板:

<?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,但都没有工作。

我用一个包含硬编码android:src的普通ImageView替换了MvxImageView,它运行良好。

有什么想法吗?

3 个答案:

答案 0 :(得分: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)" />

在这个例子中&#34; Type&#34;是一个包含单词&#34; song&#34;。

的字符串

答案 1 :(得分: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)" />

答案 2 :(得分:0)

在xml中使用:local:MvxBind =“ ImageUrl IconName” 在ViewModel中:IconName =“ res:image_name”

例如在可绘制资源图像名称中,例如“ image_name.png”

相关问题