努力使用 MvvmCross 在 MvxImageView 中绑定本地图像

2023-12-12

我似乎无法在 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,并且工作正常。

有任何想法吗?


我用它来让它为我工作:

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

在此示例中,“Type”是包含单词“song”的字符串。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

努力使用 MvvmCross 在 MvxImageView 中绑定本地图像 的相关文章

随机推荐