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

后端 未结 4 1192
走了就别回头了
走了就别回头了 2020-12-20 12:38

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

I have tried using MvxHttpImageView like this



        
4条回答
  •  心在旅途
    2020-12-20 13:16

    I built a converter, which converts a drawable name to a Bitmap and bound the Bitmap to the ImageView.

    The converter:

    public class ImageNameToBitmapConverter : MvxValueConverter
    {
        protected override Bitmap Convert(string value, Type targetType, object parameter, CultureInfo culture)
        {
    
            var topActivity = Mvx.Resolve();
    
            var bm = BitmapFactory.DecodeResource(topActivity.Activity.Resources, GetResourceId(value, "drawable", topActivity));
    
            return bm;
        }
    
        private static int GetResourceId(string variableName, string resourceName, IMvxAndroidCurrentTopActivity topActivity)
        {
            try
            {
                return topActivity.Activity.Resources.GetIdentifier(variableName, resourceName, topActivity.Activity.PackageName);
            }
            catch (Exception)
            {
                return -1;
            }
        }
    }
    

    The XML view:

    
    

    The Icon property I'm binding is a string like: "icon_name" where I put the image icon_name.png in the Android project's Resources/drawable

提交回复
热议问题