c# - 在 iPhone 5 上测试时加载图像出错

标签 c# ios xamarin

我正在尝试在 TableCell 上设置图像。

这是我的代码:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
    Parking parking = (tableItems.ToArray () [indexPath.Row] as Parking);

    if (cell == null) {
        cell = new UITableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier);
    }

    cell.TextLabel.Text = parking.Name;

    UIImage img = UIImage.FromBundle ("occupied");
    cell.ImageView.Image = img;

    cell.DetailTextLabel.Text = parking.GenerateSubtitle ();
    cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

    return cell;
}

这在模拟器上测试时完美无缺,但是当我尝试在我的 iPhone 5 上调试它时,我得到这个错误:

System.InvalidCastException: Cannot cast from source type to destination type.
  at MonoTouch.UIKit.UIImage.FromBundle (System.String name) [0x0001e] in         /Developer/MonoTouch/Source/monotouch/src/UIKit/UIImage.g.cs:198
  at parko_iphone.MainTableSource.GetCell (MonoTouch.UIKit.UITableView tableView,     MonoTouch.Foundation.NSIndexPath indexPath) [0x0004c] in /Users/gertpoppe/projects/parko_iphone/parko_iphone/CustomComponents/MainTableSource.cs:34
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIWindow.g.cs:129
  at parko_iphone.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x0002e] in /Users/gertpoppe/projects/parko_iphone/parko_iphone/AppDelegate.cs:35
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at parko_iphone.Application.Main (System.String[] args) [0x00008] in /Users/gertpoppe/projects/parko_iphone/parko_iphone/Main.cs:16

错误发生在我加载图片的那一行:

UIImage img = UIImage.FromBundle ("occupied");

最佳答案

尝试替换:

UIImage img = UIImage.FromBundle ("occupied");

与:

UIImage *img = UIImage.FromBundle ("occupied");

尝试替换:

UIImage img = UIImage.FromBundle ("occupied");
cell.ImageView.Image = img;

与:

cell.ImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"occupied"]];

关于c# - 在 iPhone 5 上测试时加载图像出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853318/

相关文章:

c# - 将非原始值分配给用户控件属性?

c# - json.net 可枚举

ios - Xcode 存档不创建 DSYM 文件

ios - 自定义 ipad 表格 View 设计

ios - 对象引用未设置为 UiTable 中对象 [Xamarin,IOS] 的实例

c# - 取消映射源属性时强制抛出异常

c# - 实现接口(interface)的类型的 Entity Framework DBSet 扩展

android - 检查现有社交媒体帐户的智能手机

c# - Xamarin.iOS 状态栏在更改其文本颜色时消失

c# - 提取 JSON 数据 Xamarin