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

Pau*_*lle 11 xamarin.android mvvmcross

我正在尝试绑定图像的src.

我试过像这样使用MvxHttpImageView

<Mvx.MvxHttpImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iconeView"
                local:MvxBind="{'ImageUrl':{'Path':'ImgSrc'}}" />
Run Code Online (Sandbox Code Playgroud)

public string ImgSrc
{
    get {return "@res/drawable/icon.png"; }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了其他几个ImgSrc但仍然没有任何结果.

icon.png位于我的Resources/Drawable目录中,是一个AndroidResource

任何帮助都会很棒!谢谢

pne*_*ook 14

较新版本的MvvmCross支持绑定到可绘制资源.使用DrawableName绑定,您可以将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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并在你的布局

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="DrawableName MyDrawable"/>
Run Code Online (Sandbox Code Playgroud)

或者,如果您的VM属性是,则可以使用Drawable绑定int


Stu*_*art 6

mvxhttpimageview知道如何从http和'disk'加载图像

可悲的是,它不知道如何从资源加载

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

  1. 您可以编写自己的自定义绑定
  2. 您可以使用标准imageview,存储在android资产中的图像以及内置于mvx中的"AssetImagePath"绑定.

要尝试第一个,请查看会议示例 - 最喜欢的按钮背景如何绑定到IsFavorite

做第二个:

  • 包括资产文件夹中的图标 - 例如/assets/icon1.png
  • 确保构建操作设置为AndroidAsset
  • 在XML中使用标准的ImageView和绑定文本,如{'AssetImagePath':{'Path':'WhichAsset'}}

在实际使用中,我一般也使用转换器 - 这是一个ViewModel财产样态与LoadingState.Loading值映射到资产图像路径,如"/loadingimages/loading.png"


您可以在https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross.Binding/Android/Target/MvxImageViewDrawableTargetBinding.cs中查看资产绑定的代码


抱歉,答案中不包含更多代码 - 在移动电话上接听