how to bind an image src to resource drawable image with Mvvmcross?

Paul Angevelle picture Paul Angevelle · Oct 13, 2012 · Viewed 13.1k times · Source

I'm trying to bind an image's src.

I have tried using MvxHttpImageView like this

<Mvx.MvxHttpImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iconeView"
                local:MvxBind="{'ImageUrl':{'Path':'ImgSrc'}}" />

with

public string ImgSrc
{
    get {return "@res/drawable/icon.png"; }
}

I have tried several other ImgSrc and still don't have any result.

icon.png is in my Resources/Drawable directory and is an AndroidResource

any help will be great ! Thanks

Answer

pnewhook picture pnewhook · Mar 28, 2014

Newer versions of MvvmCross support binding to a drawable resources. Using the DrawableName binding you can bind an ImageView to a property on your viewmodel that contains a drawable name.

using System;
using Cirrious.MvvmCross.ViewModels;

namespace MyApp.ViewModels
{
    public class UserProfileViewModel : MvxViewModel
    {    
            // set this to the name of one of the files
            // in your Resources/drawable/drawable-xxxx folders
        private MyDrawable _myDrawable;
        public string MyDrawable { 
            get { return _myDrawable; }
            set {
                _myDrawable = value;
                RaisePropertyChanged (() => MyDrawable);
            }
        }
    }
}

And in your layout

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="DrawableName MyDrawable"/>

Alternatively, you could use the Drawable binding if your VM property is an int