【问题标题】:Could not create an native instance of the type 'FWSImageRepo'. the native class hasn't been loaded无法创建“FWSImageRepo”类型的本机实例。本机类尚未加载
【发布时间】:2016-07-25 11:24:05
【问题描述】:

我正在尝试链接在内部使用 AFNetworking 的原生 iOS 框架,我最初将其构建为原型,以便将我公司的一些 ObjC IP 转移到将来也可以与 Xamarin 链接。

我首先创建了一个简单的类,它使用 AFNetworking 将小猫加载到 UIImageView 中

头文件:

#import <UIKit/UIKit.h>

@interface FWSImageRepo : NSObject
+(UIImageView *)imageViewFromURLString:(NSString *)urlString;
@end

实施

@implementation FWSImageRepo

+(UIImageView *)imageViewFromURLString:(NSString *)urlString{

    UIImageView *imageView = [[UIImageView alloc] init];

    NSURL *imageUrl= [NSURL URLWithString:@"http://www.pets4homes.co.uk/images/articles/1334/large/6-health-issues-to-watch-for-in-young-kittens-52f62cff5cabb.jpg"];

    if(imageUrl != nil) {
        [imageView setImageWithURL:imageUrl];
    }

    return imageView;
}

@end

我创建了一个框架编译器目标,并将该框架复制到 Xamarin iOS 项目中。我使用了 Objective Sharpie 来生成下面的绑定

// @interface FWSImageRepo : NSObject
[BaseType(typeof(NSObject))]
interface FWSImageRepo
{
    // +(UIImageView *)imageViewFromURLString:(NSString *)urlString;
    [Static]
    [Export("imageViewFromURLString:")]
    UIImageView ImageViewFromURLString(string urlString);
}

所有这些都按照binding project 中的说明进行链接,框架作为本机引用链接,绑定项目由单页 Xamarin iOS 应用程序引用。

现在,仅对 var fwrepo = new FWSImageRepo(); 类进行初始化就表明该框架根本不适合该项目。即使我不链接绑定项目中的框架,错误也不会改变

Could not create an native instance of the type 'FWSImageRepo': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

有什么理由检查发生了什么?我已经尝试了一切,并在网上用尽了我的搜索。就我所搜索的而言,在任何地方都没有这个要求的例子,而且几乎没有任何方向。我已经尝试过其他面临相同问题的解决方案。如何查找我出错的地方?

这是我创建的项目的链接https://drive.google.com/open?id=0B2f9RlRxZKoZcElIRkpLZnF6WVU

【问题讨论】:

    标签: objective-c xamarin.ios xamarin.ios-binding


    【解决方案1】:

    这并不能完全解决问题,但感谢@chris 让我意识到这是不加载类的问题,解决方案实际上是修改 iOS 构建设置以匹配以下选择

    我仍然只能静态访问该方法,如下所示:

    // @interface FWSImageRepo : NSObject
    [BaseType(typeof(NSObject))]
    public interface FWSImageRepo
    {
        // +(UIImageView *)imageViewFromURLString:(NSString *)urlString;
        [Static]
        [Export("imageViewFromURLString:")]
        UIImageView ImageViewFromURLString(string urlString);
    }
    

    并像这样访问它

    var imageview = FWSImageRepo.ImageViewFromURLString("");
    

    知道为什么我不能将它用作实例(我删除了 [Static] 属性吗?

    【讨论】:

    • 为了清晰静态部分。 Objective-C 中方法前面的加号表示它是类方法(也称为静态方法)。这就是为什么您只能将其绑定为静态方法。前面带减号的 Objective-C 方法是实例方法。
    【解决方案2】:

    无法创建“FWSImageRepo”类型的本机实例:本机类尚未加载。可以通过将 ObjCRuntime.Class.ThrowOnInitFailure 设置为 false 来忽略此条件。

    这表明有问题的本机库尚未加载。我会浏览文档:

    https://developer.xamarin.com/guides/ios/advanced_topics/native_interop/

    确定您的项目设置未在本机库中加载的位置。

    【讨论】:

    • 是的,这听起来对我来说也是如此,但我无法确定问题出在哪里。感谢您的链接。
    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2018-05-25
    相关资源
    最近更新 更多