Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

xamarin.android - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...