How to bind imageSource to ImageView in MvvmCross

2020-04-17 06:49发布

问题:

I'm having trouble to bind a source with converter to an imageview in my Touch project. I found here and here how to make it in Android

public class TypeToSourceConverter : MvxValueConverter<int, string>
{
    protected override string Convert (int value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var assetName = ((AppConstants.Type)value).ToString ().ToLower ();
        return "Images/" + assetName + ".png";
    }
}

But I'm having trouble in how to use this converter in iOS, i just found samples with ImageURL, I'm starting in iOS now, so I have some stupid questions as how I bind the image with this converter and where should I put the image in the Touch project! Is it in Resource/Images/*.png ?

Thanks in Regards, Gabriel


EDIT

Well, what I did, to try to solve it is: I'm passing as parameter in Droid and in iOS, a number, so in converter I do:

protected override string Convert (int value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var assetName = ((AppConstants.Type)value).ToString ().ToLower ();
        switch(int)
                    {
                    case 0://Droid
                        return "Images/" + assetName + ".png";
                    case 1://Touch
                        return "res:Images/" + assetName + ".png";
                    default:
                        return "";
    }

And in my touch Table View Cell, i have:

        var imageViewLoader = new MvxImageViewLoader (() => historicTypeImageView);
        Opaque = true;
        this.DelayBind(() => {
            var set = this.CreateBindingSet<HistoricCell, Historic>();
            set.Bind(imageViewLoader).For(i => i.DefaultImagePath).To(historic => historic.UpdateType).WithConversion("HistoricTypeToSource", 2);
            set.Apply();
        }

Now, the problem is: I figured out it was working only for the first cells, not all of them, like, it was replicating the first imageViews that was appearing for all cells, instead of bind each cell's image for each Type ...

I saw in the output, that i'm receiving:

2014-04-18 14:40:45.902 MyProjectTouch[23081:70b] MvxBind:Warning: 30.59 Weak Target is null in MvxWithEventPropertyInfoTargetBinding - skipping set

So probably, it is because of this weak target that it is skipping the bind of others cell... I'm looking for answers but didn't find yet. Actually found this this but not sure it works to fix my problem..

Can anyone help me, please!

回答1:

The solution is exactly, what Stuart suggested here:

Try making your MvxImageViewLoader a member variable of the cell - otherwise it will go out of scope and get garbage collected

Here is also part of the MVVM Cross tutorial, where the presenter runs into the same issue while live-coding and explains it by the same reasons: https://www.youtube.com/watch?v=iFEE355587k#t=1443