How to dynamically change image in MvvmCross

你说的曾经没有我的故事 提交于 2020-01-03 17:56:25

问题


I have both images in the resources folder

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

in code Behind:

when this page is loaded, it first displays the said image : ImgMsg_Normal.

1) How to change the image dynamically by passing image filename: ImgMsg_Grey in Local : MvxBind above?

Thanks


回答1:


We do that with a converter. So the binding is on a (for example) boolean value. If its true the converter returns the image 1 and if the value is false, he returns the image 2.

The Converter (in the Core-Project):

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

And the binding in your file:

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

With above code we change the icon dynamically in a list who shows different items. The icon depends on a property of the item in the list.



来源:https://stackoverflow.com/questions/32090067/how-to-dynamically-change-image-in-mvvmcross

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!