Fight for binding local images in MvxImageView with MvvmCross

I can't get images to snap correctly in MvxListView

Here is the template:

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

Here is the converter:

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

All images are present in the Drawable folder.

I tried both drawable and res and did not work.

I replaced MvxImageView with a simple ImageView containing hard coded Android: src, and it worked fine.

Any ideas?

+4
source share
2 answers

I used this to make it work for me:

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

To use this in an Android layout:

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

In this example, “Type” is a string containing the word “song”.

+4
source

Thanks, it works. Tried with 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 layout

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

Source: https://habr.com/ru/post/1531696/


All Articles