Android – how to bind mvvmcross to drawviewid to ImageView
•
Android
How to return drawableid in the converter?
My layout file:
<ImageView
android:id="@+id/imgTest"
android:src="@drawable/Img_Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:layout_margin="20dp"
android:scaleType="fitCenter"
local:MvxBind ="DrawableId MySwapImage(true)" />
I have two images in the resource folder
> ImgTest1.png > ImgTest2.png
My value converter:
public class MySwapImageValueConverter : MvxValueConverter<bool, int>
{
protected override int Convert(bool value, Type targetType, object parameter, CultureInfo culture)
{
if(value)
{
// ImgTest1
}
else
{
// ImgTest2
}
}
}
resolvent:
Your problem is that this converter should not be located in your. Core project (the project where your VM is located). This code is platform specific because it changes the Boolean value (content related to business logic) to drawable (content existing only on Android). Therefore, it should belong to your. Droid project
In my project, I create a converters folder in the droid project to store all my platform specific converters. The code of myswapimagevalueconverter should be as follows
using System;
using System.Globalization;
using CirrIoUs.Crosscore.Converters;
namespace MilkBottle.Droid.Converters
{
public class MySwapImageValueConverter : MvxValueConverter<bool, int>
{
#region Methods
protected override int Convert(bool value, Type targetType, object parameter, CultureInfo culture)
{
var result = value ? Resource.Drawable.ImgTest1: Resource.Drawable.ImgTest2;
return result;
}
#endregion
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码