如何在 MvvmCross 中动态更改图像

How to dynamically change image in MvvmCross

资源文件夹中有两张图片

1) ImgMsg_Normal.png
2) ImgMsg_Grey.png

in Layout file:

<ImageView

    android:id="@+id/imgMsg"

    android:src="@drawable/ImgMsg_Normal"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:maxHeight="80dp"
    android:maxWidth="80dp"   
    android:layout_margin="20dp"   
    android:scaleType="fitCenter"

    local:MvxBind ="      " />

在后面的代码中:

加载此页面时,它首先显示所述图像:ImgMsg_Normal。

1) 如何通过传递图像文件名动态更改图像:ImgMsg_Grey 在 Local : MvxBind 上面?

谢谢

我们使用转换器来做到这一点。所以绑定在一个(例如)布尔值上。如果它是真的转换器 returns 图像 1,如果值是假的,他 returns 图像 2.

转换器(在核心项目中):

public class MyIconValueConverter : MvxValueConverter<bool, string>
{
    protected override string Convert(bool value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value)
        {
            return "res:ImgMsg_Normal";
        }
        else
        {
            return "res:ImgMsg_Grey";
        }
    }
}

以及您文件中的绑定:

<Mvx.MvxImageView 
    local:MvxBind="ImageUrl MyBoolProperty, Converter=MyIcon" />

使用上面的代码,我们在显示不同项目的列表中动态更改图标。图标取决于列表中项目的 属性。