绑定iOS嵌入式框架Xamarin END UP会给出异常吗? [英] Binding iOS embedded Framework for Xamarin END UP give exception?
问题描述
请帮助我完成最后一步. 谢谢.
Please help my last step. Thanks.
我正在测试将iOS本机框架绑定到Xamarin库.
I'm testing binding iOS native framework to Xamarin library.
-
我创建了iOS框架并添加了MyView.h,MyView.m
I created iOS framework and added MyView.h, MyView.m
- MyView.m
(实例类型)initWithFrame:(CGRect)框架
{
id p = [super initWithFrame:frame];
self.backgroundColor = [UIColor greenColor];
返回p;
}
(instancetype)initWithFrame:(CGRect)frame
{
id p = [super initWithFrame:frame];
self.backgroundColor = [UIColor greenColor];
return p;
}
- MyView.h
@interface MyView:UIView @end
@interface MyView : UIView @end
非常简单的框架.
-
将该MyView.h添加到Public Header中,并为任何体系结构制作通用框架. 该框架现在具有
add that MyView.h into Public Header and Made universal framework for any architecture. This framework now has
firstfile.h,MyView.h
firstfile.h, MyView.h
两个文件作为公共头文件
two files as public headers
-
我创建了iOS测试应用(单个VC应用)并尝试了此框架.
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];
MyView *v = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [self.view addSubview:v];
它按我的预期工作.它画了一个绿色的盒子.
It worked as I expected. It drew a green box.
-
我创建了Xamarin库项目,并通过单击鼠标右键并添加文件将其导入到本机引用文件夹中
I created Xamarin Library project and import this framework file on Native Reference Folder by clicking right click and add file
将此行添加到Xamarin库中的ApiDefinition.cs
Add this lines to ApiDefinition.cs in Xamarin Library
[BaseType(typeof(UIView))] 界面MyView { [导出("initWithFrame:")] IntPtr构造函数(CGRect框架); }
[BaseType(typeof(UIView))] interface MyView { [Export ("initWithFrame:")] IntPtr Constructor (CGRect frame); }
建立OK.
-
我创建了Xamarin.iOS应用,并将该库导入解决方案并进行了测试.
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);
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.
由于我是CSharp和mono的新手. 来这里并不容易, 此时您能帮我一点忙吗?
As I'm new with CSharp and mono. It was not easy to come here, Could you help me little bit at this point?
我的线索是,应该在第5步中指向MyView.h.但是我不知道那里应该有什么.
My clue is that there should be something pointing MyView.h in No.5 step. But I don't know what should be there.
我的完整源代码在这里: https://github.com/myallb/test_iosstaticlib_to_xamarin
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.
推荐答案
框架绑定示例....
An example framework binding....
- 添加一个名为
MyView
的新可可接触类,并将其归类为UIView
:
- Add a new Cocoa Touch Class named
MyView
subclassingUIView
:
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示例(开始启动ApiDefinition.cs
)
sharpie bind
-sdk iphoneos9.3
-o GreenViewBinding
GreenView.framework/Headers/*.h
创建一个Xamarin.iOS
绑定项目:
- 添加
GreenView.framework
作为本机引用 - Add the
GreenView.framework
as a Native Reference
Create a Xamarin.iOS
Binding Project:
ApiDefinition.cs
using UIKit;
namespace GreenViewBinding
{
// @interface MyView : UIView
[BaseType(typeof(UIView))]
interface MyView
{
}
}
用法:
-
创建iOS单视图应用
Usage:
Create a iOS single view app
将Binding项目添加为参考.
Add the Binding project as a reference.
ViewController.cs
ViewDidLoad
方法:public override void ViewDidLoad() { base.ViewDidLoad(); var view = new GreenViewBinding.MyView(); view.Frame = new CoreGraphics.CGRect(40, 40, 100, 100); Add(view); }
输出:
这篇关于绑定iOS嵌入式框架Xamarin END UP会给出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!