user1202032
user1202032

Reputation: 1479

MVVMCross binding ImageView to android resource ID instead of assets path

I have a layout with an ImageView in it. I want to show a different image, depending on logic in my core project. Adding the images to the assets folder and binding it up with a converter works. For my ValueConverter:

public class MyImageValueConverter : MvxValueConverter<MyMap, string>
{

    protected override string Convert(MyMap value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return GetResourcePath(value);
    }

    private string GetResourcePath(MyMap map)
    {   
        string path = "";

        switch (map)
        {
            case MyMap.Map1:
                path = "map1.png";
                break;
            case MyMap.Map2:
                path = "map2.png";
                break;
            default:
                throw new Exception("no image for map: " + map.ToString());
        }

        return path;
    }
}

In my view, i bind it up like this:

...
var map = FindViewById<MvxImageView>(Resource.Id.my_imageview);
set.Bind(map).For("AssetImagePath").To(vm => vm.CurrentMap).WithConversion("MyImage");
...

Now to my question. I would very much prefer to set the image from a resource drawable instead, but i have not found a way to do this. I feel like i should just be able to return a resource ID from my converter, and bind it to some property (which?).

Alternatively i could just create a new class that inherits from MvxImageView and add a property that i could do a one-way binding to, that calls SetImageResource(). Or even go the full custom binding way. None of these seem very nice.

Upvotes: 0

Views: 1134

Answers (1)

WriteEatSleepRepeat
WriteEatSleepRepeat

Reputation: 3143

Bind to MvxImageView's DrawableId

Look to MvxAndroidBindingBuilder, it has some hidden gems:

https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs#L103

Upvotes: 2

Related Questions