1

使用 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在实现协议中的所有方法时会失败。我在这里想念什么?

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

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

1 回答 1

2

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

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

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

于 2013-08-14T14:06:49.300 回答