绑定协议无法通过conformsToProtocol

时间:2013-08-14 13:25:16

标签: objective-c xamarin janrain

使用Xamarin为一些外部代码创建一个绑定库,我遇到了一个问题,即尽管(据我所知)在协议中实现所有选择器,在Obj-C代码中,我的委托无法通过conformsToProtocol检查,结果代码不起作用。

这是Objective-C头文件的样子(https://github.com/janrain/jump.ios/blob/master/Janrain/JRCapture/Classes/JRCapture.h):

@protocol JRCaptureDelegate <NSObject>
@optional
- (void)engageAuthenticationDialogDidFailToShowWithError:(NSError *)error;

- (void)engageAuthenticationDidCancel;

- (void)engageAuthenticationDidSucceedForUser:(NSDictionary *)engageAuthInfo forProvider:(NSString *)provider;

 - (void)engageAuthenticationDidFailWithError:(NSError *)error forProvider:(NSString *)provider;

- (void)captureSignInDidSucceedForUser:(JRCaptureUser *)captureUser
                            status:(JRCaptureRecordStatus)captureRecordStatus;

- (void)captureSignInDidFailWithError:(NSError *)error;

- (void)registerUserDidSucceed:(JRCaptureUser *)registeredUser;

- (void)registerUserDidFailWithError:(NSError *)error;

- (void)refreshAccessTokenDidSucceedWithContext:(id <NSObject>)context;

- (void)refreshAccessTokenDidFailWithError:(NSError *)error context:(id <NSObject>)context;
@end

这是与ApiDefinition.cs对应的部分:

[Model, BaseType (typeof (NSObject))]
public partial interface JRCaptureDelegate {

    [Export ("engageAuthenticationDidCancel")]
    void EngageAuthenticationCancelled ();

    [Export ("engageAuthenticationDidSucceedForUser:forProvider:")]
    void AuthenticationSucceededForProvider (NSDictionary engageAuthInfo, string provider);

    [Export ("captureSignInDidSucceedForUser:status:")]
    void SignInSucceeded (JRCaptureUser captureUser, JRCaptureRecordStatus captureRecordStatus);

    [Export ("registerUserDidSucceed:")]
    void  RegisterUserSucceeded(JRCaptureUser registeredUser);

    [Export ("refreshAccessTokenDidSucceedWithContext:")]
    void  RefreshAccessTokenSucceeded(NSObject context);

    //- (void)engageAuthenticationDialogDidFailToShowWithError:(NSError *)error
    [Export ("engageAuthenticationDialogDidFailToShowWithError:")]
    void DialogFailedToShow (NSError error);

    //- (void)engageAuthenticationDidFailWithError:(NSError *)error forProvider:(NSString *)provider;
    [Export("engageAuthenticationDidFailWithError:forProvider:")]
    void AuthenticationFailedForProvider (NSError error, NSString provider);

    //- (void)captureSignInDidFailWithError:(NSError *)error;
    [Export("captureSignInDidFailWithError:")]
    void SignInFailed (NSError error);

    //- (void)registerUserDidFailWithError:(NSError *)error;
    [Export("registerUserDidFailWithError:")]
    void RegisterUserFailed (NSError error);

    //- (void)refreshAccessTokenDidFailWithError:(NSError *)error context:(id <NSObject>)context;
    [Export("refreshAccessTokenDidFailWithError:context:")]
    void RefreshAccessTokenFailed (NSError error, NSObject context);
}

这编译并且似乎工作得很好,除了我遇到一个特殊方法的问题,似乎不想调用我的委托类中的方法。经过很多的挖掘(以及艰难的学习Objective-C)之后,我相信我已经解决了这个问题。在图书馆我有约束力,我有这个:

    if ([delegate conformsToProtocol:@protocol(JRCaptureDelegate)] &&
        [delegate respondsToSelector:@selector(captureSignInDidSucceedForUser:status:)])

通过一点DLog,我发现我的委托未通过conformsToProtocol检查(尽管它确实通过了respondsToSelector检查)。

那么为什么我的类在实现协议中的所有方法时失败conformsToProtocol。我在这里缺少什么?

我的类实现委托,该委托传递给采用JRCaptureDelegate的各种方法,如下所示:

public class SignIn : JRCaptureDelegate
{
     // overrides for each method
}

1 个答案:

答案 0 :(得分:2)

好吧,我想我可能已经破解了它。我在我的类中添加了Adopts属性(在Xamarin文档中的各个地方都含糊不清地提到了这一点,但在创建绑定库时并没有真正建议这样做)。所以现在我有了这个:

[Adopts("JRCaptureDelegate")] 
public class SignIn : JRCaptureDelegate
{
     // overrides for each method
}

它现在通过conformsToProtocol检查。我不确定为什么这不是自动的,因为我正在实现JRCaptureDelegate接口/协议。