Xamarin iOS delegate BaseType usage

我的未来我决定 提交于 2019-12-08 06:13:28

问题


I'm just wondering why delegate in binding iOS project has to use BaseType(typeof(NSObject)) attribute when it's iOS counterpart does not use NSObject

iOS code:

@protocol TestDelegate

- (void)onSuccess:(NSString*)token;

@end

@interface Utility : NSObject

@property (nullable, weak, getter = getTestDelegate, setter = setTestDelegate:) id<TestDelegate> delegate;

@end

Sharpie code with delegate to event mapping added:

[Protocol, Model]
public interface TestDelegate
{
    [Export ("onSuccess:")]
    void OnSuccess (string token);
}

[BaseType(typeof(NSObject),
      Delegates = new string[] { "WeakDelegate" },
      Events = new Type[] { typeof(TestDelegate) })
public interface Utility
{
    [Wrap ("WeakDelegate")]
    [NullAllowed]
    TestDelegate Delegate { [Bind ("getTestDelegate")] get; [Bind ("setTestDelegate:")] set; }

    [NullAllowed, Export ("delegate", ArgumentSemantic.Weak)]
    NSObject WeakDelegate { [Bind ("getTestDelegate")] get; [Bind ("getTestDelegate:")] set; }
}

BaseType attribute was not generated on TestDelegate by Sharpie because iOS native code was not using <NSObject> in its protocol.

This fails with "The type or namespace name TestDelegate' does not exist in the namespaceTest'. Are you missing an assembly reference? (CS0234) (Test.iOS)".

When I add [BaseType(typeof(NSObject))] on top of the TestDelegate it works like a charm.

The question is why this is needed?

来源:https://stackoverflow.com/questions/43126822/xamarin-ios-delegate-basetype-usage

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