Xamarin for Objective-C开发者
2017-09-06 本文已影响28人
LeeCen
image.png
- 属性
Objective-C
@property (nonatomic, copy) NSString *str;
C#
public string Str { get; set; }
- Get Set
Objective-C
- (void)setStr:(NSString *)str
C#
string str;
public string Str {
get {
return str;
}
set {
str = value;
}
}
- 泛型
Objective-C
MyTextField *myTextField = [[MyTextField alloc] init];
MyTextField *myTextField2 = [[MyTextField alloc] init];
NSArray<MyTextField *> *array = @[myTextField, myTextField2];
C#
MyEntry entry1 = new MyEntry();
MyEntry entry2 = new MyEntry();
List<MyEntry> myList = new List<MyEntry> { entry1, entry2 };
- 自定义类初始化方法
Objective-C
//初始化
- (instancetype)init {
self = [super init];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
//带参数的初始化
- (instancetype)initWithStr:(NSString *)str {
self = [super init];
if (self) {
self.backgroundColor = [UIColor redColor];
self.text = str;
}
return self;
}
C#
public class MyEntry : Entry
{
//初始化
public MyEntry()
{
this.BackgroundColor = Color.Red;
}
//带参数的初始化
public MyEntry(string str){
this.BackgroundColor = Color.Red;
this.Text = str;
}
}
- 分类
Objective-C
#import "UITextField+Extensions.h"
@implementation UITextField (Extensions)
- (void)textStr:(NSString *)str {
self.text = str;
}
@end
C#
public static class EntryExtensions
{
public static void TextStr(this Entry entrt, string str)
{
entrt.Text = str;
}
}
- 动画
Objective-C
[UIView animateWithDuration:1.0 animations:^{
self.view.alpha = 0.0;
}];
C#
Animate(1.0, () => {
self.view.alpha = 0.0;
});