Binding iOS embedded Framework for Xamarin END UP give exception?

独自空忆成欢 提交于 2020-01-03 03:04:08

问题


Please help my last step. Thanks.

I'm testing binding iOS native framework to Xamarin library.

  1. I created iOS framework and added MyView.h, MyView.m

    • MyView.m

    (instancetype)initWithFrame:(CGRect)frame { id p = [super initWithFrame:frame]; self.backgroundColor = [UIColor greenColor];
    return p; }

    • MyView.h

    @interface MyView : UIView @end

very simple framework.

  1. add that MyView.h into Public Header and Made universal framework for any architecture. This framework now has

    firstfile.h, MyView.h

two files as public headers

  1. I created iOS test app (single VC app) and tried this framework.

    MyView *v = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [self.view addSubview:v];

It worked as I expected. It drew a green box.

  1. I created Xamarin Library project and import this framework file on Native Reference Folder by clicking right click and add file

  2. Add this lines to ApiDefinition.cs in Xamarin Library

    [BaseType(typeof(UIView))] interface MyView { [Export ("initWithFrame:")] IntPtr Constructor (CGRect frame); }

build OK.

  1. I created Xamarin.iOS app and import this library to solution and test it.

    MyView v = new MyView(new CGRect(0, 0, 100, 100)); this.View.AddSubview(v);

BUILD OK! but runtime error.

Exception has been thrown by the target of an invocation.

As I'm new with CSharp and mono. It was not easy to come here, Could you help me little bit at this point?

My clue is that there should be something pointing MyView.h in No.5 step. But I don't know what should be there.

my full source code is here : https://github.com/myallb/test_iosstaticlib_to_xamarin

This code is using Static Library instead framework. I was trying different option.


回答1:


An example framework binding....

iOS-based GreenView Universal Framework (via Xcode):

  • Add a new Cocoa Touch Class named MyView subclassing UIView:

MyView.h:

#import <UIKit/UIKit.h>

@interface MyView : UIView {
}

@end

MyView.m:

#import "GreenView/MyView.h"

@implementation MyView

- (void)baseInit {
    self.backgroundColor = [UIColor greenColor];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self baseInit];
    }
    return self;
}

@end

GreenView.h:

#import <UIKit/UIKit.h>

FOUNDATION_EXPORT double GreenViewVersionNumber;
FOUNDATION_EXPORT const unsigned char GreenViewVersionString[];

#import "MyView.h"

Sharpie Example (to jump start the ApiDefinition.cs)

sharpie bind 
   -sdk iphoneos9.3 
   -o GreenViewBinding 
   GreenView.framework/Headers/*.h

Create a Xamarin.iOS Binding Project:

  • Add the GreenView.framework as a Native Reference

ApiDefinition.cs

using UIKit;

namespace GreenViewBinding
{
    // @interface MyView : UIView
    [BaseType(typeof(UIView))]
    interface MyView
    {
    }
}

Usage:

  • Create a iOS single view app

  • Add the Binding project as a reference.

ViewController.cs ViewDidLoad method:

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    var view = new GreenViewBinding.MyView();
    view.Frame = new CoreGraphics.CGRect(40, 40, 100, 100);
    Add(view);
}

Output:



来源:https://stackoverflow.com/questions/38963497/binding-ios-embedded-framework-for-xamarin-end-up-give-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!