Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvxImageView, can't bind ImageUrl to local resource

I'm using MvvmCross 3.0.14 with Xamarin.Android.

I have an MvxImageView that I can get to display a particular local graphics resource if I specify the image directly (without binding) using android:src:

<Mvx.MvxImageView
  android:layout_width="100dp"
  android:layout_height="75dip"
  android:layout_weight="1"
  android:src="@drawable/Card_kh"/>

But I can't get the same image to show up using local:MvxBind:

<Mvx.MvxImageView
  android:layout_width="100dp"
  android:layout_height="75dip"
  android:layout_weight="1"
  local:MvxBind="ImageUrl 'res:Card_kh'"/>

This does not work. MvxAndroidLocalFileImageLoader.LoadResourceBitmap logs a trace message indicating that 'Card_kh' was not a known drawable name. It's encouraging that it got that far -- at least I know that the intended consumer of this information did get it. But apparently I have not provided that information in the correct format.

Taking it one step further, my actual goal is to have my ViewModel determine what resource should be used, e.g.

class MyViewModel : MvxViewModel
{
  public string SomeImagePath { get { return "res:Card_kh"; } }
}

and

<Mvx.MvxImageView
  android:layout_width="100dp"
  android:layout_height="75dip"
  android:layout_weight="1"
  local:MvxBind="ImageUrl SomeImagePath"/>

What do I need to do to have an MvxImageView bind to a view-model determined local resource image?

like image 393
Tim Crews Avatar asked Jan 05 '14 23:01

Tim Crews


2 Answers

The problem was only capitalization in the resource name. Although the image filename starts with a capital C, and the android:src attribute works with a capital C, the MvxBind of ImageUrl requires a lowercase c:

<Mvx.MvxImageView
  android:layout_width="100dp"
  android:layout_height="75dip"
  android:layout_weight="1"
  local:MvxBind="ImageUrl 'res:card_kh'"/>

This also solves the problem when the source of the ImageUrl value is a viewmodel property:

class MyViewModel : MvxViewModel
{
  public string SomeImagePath { get { return "res:card_kh"; } }
}

and

<Mvx.MvxImageView
  android:layout_width="100dp"
  android:layout_height="75dip"
  android:layout_weight="1"
  local:MvxBind="ImageUrl SomeImagePath"/>
like image 167
Tim Crews Avatar answered Oct 22 '22 15:10

Tim Crews


The previous answer is not working anymore since MVVMCross v6.0.0.

As described here https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60 MvxImageView has been removed from the API.

The newest alternative is now https://github.com/luberda-molinet/FFImageLoading/wiki/MvvmCross.

This nuget package "Xamarin.FFImageLoading" must be added in order to make the code below work.

<ffimageloading.cross.MvxCachedImageView
  android:layout_width="100dp"
  android:layout_height="75dip"
  android:layout_weight="1"
  local:MvxBind="ImagePath SomeImagePath"/>
like image 26
Robin Delgado Avatar answered Oct 22 '22 15:10

Robin Delgado