2013-06-01 20 views
1

我正在尝试在用Xamarin和MvvmCross构建的iPhone应用程序中显示MvxCollectionViewCell的背景图像。我针对iOS 6.0及更高版本。以下是我正在使用的代码。我在获取背景图片时遇到了一些问题。我似乎无法弄清楚我做错了什么。似乎它应该相当简单。我在XCode中创建了单元格。单元格为100 X 100.它包含一个单独的UIImageView,它是90 X 72. UIImageView的X和Y分别是5和8。我也无法让图像在单元格内调整大小,但计划很快发布另一个问题。MvxCollectionViewCell的显示背景

using MonoTouch.UIKit; 
using Cirrious.MvvmCross.Binding.Touch.Views; 
using Cirrious.MvvmCross.Binding.BindingContext; 

namespace DriveThroughSafari.Touch.Views.Pictures 
{ 
public sealed partial class AnimalCutoutCell : MvxCollectionViewCell 
{ 
    public static readonly UINib Nib = UINib.FromName ("AnimalCutoutCell", NSBundle.MainBundle); 
    public static readonly NSString Key = new NSString ("AnimalCutoutCell"); 

    private readonly MvxImageViewLoader _loader; 

    public AnimalCutoutCell (IntPtr handle) : base (handle) 
    { 

     MainImage = new UIImageView(); 
     _loader = new MvxImageViewLoader(() => MainImage); 

     BackgroundView = new UIImageView {Image = new UIImage("Images/photo-frame")}; 

     this.DelayBind(() => 
      { 
       var set = this.CreateBindingSet<AnimalCutoutCell, AnimalDataViewModel>(); 
       set.Bind(_loader).To(animal => animal.ImageUrl); 
       set.Apply(); 
      }); 
    } 

    public static AnimalCutoutCell Create() 
    { 
     return (AnimalCutoutCell)Nib.Instantiate (null, null) [0]; 
    } 
    } 
} 

我很感谢帮助!

+0

这可能是因为当笔尖加载时,你的背景被覆盖 - 你可以使用一个覆盖 - 就像装在笔尖上的东西。一如既往地在莫尔 – Stuart

回答

0

我试图在修改示例:https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-11-KittenView_Collections

我改变了代码以符合您的 - 包括这条线在构造函数中:

BackgroundView = new UIImageView {Image = new UIImage("MyImage.png")}; 

当图像MyImage.png被列入与生成操作content Touch项目的根。

这工作得很好。


我怀疑这里的第一步是写一些测试代码和使用调试器 - 这是什么var myImage = UIImage.FromFile("Images/photo-frame")回报呢?这是一个有效的UIImage吗?

+0

斯图尔特不正常在线,我欣赏真棒反馈!事实证明,我的图像比UIImageView大得多,而不是调整大小以适应视图内部,它只占用了整个单元格。用正确的尺寸创建图像解决了问题。任何想法自动调整图像大小以适应UIImageView?正如你上面看到的,我使用MvxImageViewLoader来加载数据。 – blakeD

+0

真正的新问题(帮助搜索) - 但我认为你会像找到答案一样容易找到答案...搜索'UIImageView'和'scale' - 我相信你'会找到答案... – Stuart