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
{
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(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.
Solutions1
Bind to MvxImageView's DrawableId
Look to MvxAndroidBindingBuilder, it has some hidden gems:
Talk1:
Thank you! The BindingBuilder is an excellent resource to discover these otherwise somewhat "hidden" binding properties