Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 931
Paul Angevelle Avatar asked Oct 13 '12 19:10

Paul Angevelle


2 Answers

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

like image 161
pnewhook Avatar answered Sep 18 '22 15:09

pnewhook


For latest MvvmCross next solution is actual:

In markup:

<ImageView
    android:id="@+id/iconImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_centerVertical="true"
    local:MvxBind="DrawableName Type, Converter=TypeToImageStringConverter" />

In converter:

public class TypeToImageStringConverter : MvxValueConverter<VacationType, int>
{
    protected override int Convert(VacationType value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        switch (value)
        {
            case VacationType.RegularVacation:
                return Resource.Drawable.Icon_Request_Green;
            case VacationType.SickLeave:
                return Resource.Drawable.Icon_Request_Blue;
            case VacationType.LeaveWithoutPay:
                return Resource.Drawable.Icon_Request_Dark;
            case VacationType.OvertimeVacation:
                return Resource.Drawable.Icon_Request_Gray;
            case VacationType.ExceptionalLeave:
                return Resource.Drawable.Icon_Request_Plum;
            default:
                return Resource.Drawable.Icon_Request_Gray;
        }
    }
}

All sense is that you just must give out a drawable resource id.

like image 21
ukod Avatar answered Sep 18 '22 15:09

ukod