Xamarin.android 如何使用Mvvmcross将图像src绑定到资源可绘制图像?

Xamarin.android 如何使用Mvvmcross将图像src绑定到资源可绘制图像?,xamarin.android,mvvmcross,Xamarin.android,Mvvmcross,我正在尝试绑定图像的src 我尝试过像这样使用MvxHttpImageView <Mvx.MvxHttpImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iconeView" local:MvxBind="{'Ima

我正在尝试绑定图像的src

我尝试过像这样使用MvxHttpImageView

<Mvx.MvxHttpImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iconeView"
                local:MvxBind="{'ImageUrl':{'Path':'ImgSrc'}}" />
我尝试过其他几种ImgSrc,但仍然没有任何结果

icon.png位于my Resources/Drawable目录中,是AndroidResource

任何帮助都会很好!
谢谢

mvxhttpimageview知道如何从http和“磁盘”加载图像

遗憾的是,它不知道如何从资源中加载

但是,有一些方法可以让图像加载静态内容

  • 您可以编写自己的自定义绑定
  • 您可以使用标准的imageview、android资产中存储的图像以及mvx内置的“AssetImagePath”绑定
  • 要尝试第一种方法,请查看会议示例—favorite按钮背景是如何绑定到IsFavorite的

    要执行第二步:

    • 在资产文件夹中包括图标-例如/assets/icon1.png
    • 确保构建操作设置为AndroidAsset
    • 在XML中,使用标准的ImageView和绑定文本,如{'AssetImagePath':{'Path':'WhichAsset'}
    在实际使用中,我通常也会使用一个转换器,它将viewmodel属性(如State)与LoadingState.Loading的值映射到资产图像路径(如“/loadingimages/Loading.png”)


    您可以在中看到资产绑定的代码



    抱歉回答不包括更多的手机代码应答

    MvvmCross新版本支持绑定到可绘制资源。使用,可以将ImageView绑定到viewmodel上包含可绘制名称的属性

    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);
                }
            }
        }
    }
    
    在你的布局中

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            local:MvxBind="DrawableName MyDrawable"/>
    
    
    

    或者,您可以使用if-your-VM属性是一个
    int

    我构建了一个转换器,它将可绘制名称转换为位图,并将位图绑定到ImageView

    转换器:

    public class ImageNameToBitmapConverter : MvxValueConverter<string, Bitmap>
    {
        protected override Bitmap Convert(string value, Type targetType, object parameter, CultureInfo culture)
        {
    
            var topActivity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>();
    
            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;
            }
        }
    }
    
    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;
            }
        }
    }
    
    公共类ImageNameToBitmapConverter:MvxValueConverter
    {
    受保护的覆盖位图转换(字符串值、类型targetType、对象参数、CultureInfo区域性)
    {
    var topActivity=Mvx.Resolve();
    var bm=BitmapFactory.DecodeResource(topActivity.Activity.Resources,GetResourceId(值,“drawable”,topActivity));
    返回bm;
    }
    私有静态int-GetResourceId(字符串变量名、字符串资源名、IMvxAndroidCurrentTopActivity-topActivity)
    {
    尝试
    {
    返回topActivity.Activity.Resources.GetIdentifier(variableName、resourceName、topActivity.Activity.PackageName);
    }
    捕获(例外)
    {
    返回-1;
    }
    }
    }
    
    XML视图:

    <ImageView
        local:MvxBind="Bitmap Icon, Converter=ImageNameToBitmap"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    
    
    

    我绑定的
    Icon
    属性是一个字符串,类似于:“Icon\u name”,我将图像
    Icon\u name.png
    放在Android项目的
    资源/drawable

    中,用于最新的MvvmCross下一个解决方案是实际的:

    在标记中:

    <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" />
    
    
    
    在转换器中:

    public class ImageNameToBitmapConverter : MvxValueConverter<string, Bitmap>
    {
        protected override Bitmap Convert(string value, Type targetType, object parameter, CultureInfo culture)
        {
    
            var topActivity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>();
    
            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;
            }
        }
    }
    
    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;
            }
        }
    }
    
    公共类TypeToImageStringConverter:MvxValueConverter
    {
    受保护的重写int转换(VacationType值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
    {
    开关(值)
    {
    案例VacationType.Regulation:
    返回Resource.Drawable.Icon\u Request\u绿色;
    案例休假类型。病假:
    返回Resource.Drawable.Icon\u Request\u Blue;
    案例VacationType.LeaveWithoutPay:
    返回Resource.Drawable.Icon\u Request\u暗;
    案例VacationType.OvertimeVacation:
    返回Resource.Drawable.Icon\u Request\u灰色;
    案例休假类型。例外休假:
    返回Resource.Drawable.Icon\u Request\u Plum;
    违约:
    返回Resource.Drawable.Icon\u Request\u灰色;
    }
    }
    }
    

    所有的意义都是,您只需提供一个可绘制的资源id。

    谢谢,它与asset和AssetImagePath的功能类似。为了防止对任何人有帮助,/assets/不得包含在映像路径中。公共字符串ImgSrc{get{return“icon.png”;}}}请记住,_myDrawable不应包含大写字母,即使文件名包含大写字母。请更新这些断开的链接,好吗?提前谢谢!别介意我的回答@pnewook的解决方案更干净:PI喜欢你的解决方案,因为你可以从pcl设置图像!我对你的GetResourceId有一些问题。返回值始终为0。我的解决方案是使用反射:return(int)typeof(Resource.Drawable).GetField(value).GetValue(null);