如何在 MvvmCross 中将 imageSource 绑定到 ImageView [英] How to bind imageSource to ImageView in MvvmCross

查看:26
本文介绍了如何在 MvvmCross 中将 imageSource 绑定到 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将带有转换器的源绑定到我的 Touch 项目中的图像视图.我发现 here这里如何在Android中制作

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";
    }
}

但是我在如何在 iOS 中使用这个转换器时遇到了麻烦,我刚刚找到了带有 ImageURL 的示例,我现在开始在 iOS 中,所以我有一些愚蠢的问题,比如我如何将图像与这个转换器绑定以及在哪里我应该把图像放在 Touch 项目中吗?它在 Resource/Images/*.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

好吧,我所做的,试图解决它是:我在 Droid 和 iOS 中作为参数传递一个数字,所以在转换器中我这样做:

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();
        }
        

现在,问题是:我发现它仅适用于第一个单元格,而不是所有单元格,例如,它复制了出现在所有单元格中的第一个 imageViews,而不是为每个类型绑定每个单元格的图像...

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

谁能帮帮我,拜托!

推荐答案

解决方案正是 Stuart 在这里建议的:

The solution is exactly, what Stuart suggested here:

尝试让您的 MvxImageViewLoader 成为单元格的成员变量 - 否则它将超出范围并被垃圾收集

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

这也是 MVVM Cross 教程的一部分,演示者在实时编码时遇到了同样的问题,并以同样的原因进行了解释:https://www.youtube.com/watch?v=iFEE355587k#t=1443

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

这篇关于如何在 MvvmCross 中将 imageSource 绑定到 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆