MvxImageView, can't bind ImageUrl to local resource
Asked Answered
B

2

5

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?

Bierman answered 5/1, 2014 at 23:3 Comment(0)
B
8

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"/>
Bierman answered 6/1, 2014 at 4:25 Comment(0)
R
2

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"/>
Rms answered 24/5, 2018 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.