MvxImageView不能将ImageUrl绑定到本地资源。

时间:2022-06-10 20:54:20

I'm using MvvmCross 3.0.14 with Xamarin.Android.

我使用的是MvvmCross 3.0.14和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:

我有一个MvxImageView,如果我用android直接指定图像(不绑定),我可以显示一个特定的本地图形资源:

<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:

但是我不能使用本地的: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.

这并不工作。MvxAndroidLocalFileImageLoader。LoadResourceBitmap记录了一个跟踪消息,表明“Card_kh”不是一个已知的drawable名称。令人鼓舞的是,它得到了这么多——至少我知道这个信息的目标消费者确实得到了它。但显然我没有提供正确格式的信息。

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?

我需要做什么才能使MvxImageView绑定到视图模型确定的本地资源映像?

1 个解决方案

#1


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:

问题只是资源名称中的大写。尽管图像文件名以大写C开头,而android:src属性与大写C一起工作,ImageUrl的MvxBind需要小写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:

这也解决了ImageUrl值的源是viewmodel属性时的问题:

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"/>

#1


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:

问题只是资源名称中的大写。尽管图像文件名以大写C开头,而android:src属性与大写C一起工作,ImageUrl的MvxBind需要小写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:

这也解决了ImageUrl值的源是viewmodel属性时的问题:

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"/>