每当从我的Objective-C库调用我的委托的Canceled
方法时,我都会抛出异常。
ObjCRuntime.RuntimeException: Could not find the method 'Canceled' in the type 'MCImagePicker+_MCImagePickerDelegate'. at ObjCRuntime.Runtime.FindMethod (IntPtr typeptr, IntPtr methodptr, Int32 paramCount, System.IntPtr* paramptr)
它表示找不到该方法,但是该方法是在绑定中定义的。这是我使用的绑定:
// @protocol MCImagePickerDelegate <NSObject>
[Protocol, Model]
[BaseType(typeof(NSObject))]
interface MCImagePickerDelegate
{
// @optional -(void)imagePicker:(MCImagePicker *)imagePicker pickedImage:(UIImage *)image;
[Export("imagePicker:pickedImage:"), EventArgs("ImagePicked")]
void PickedImage(MCImagePicker imagePicker, UIImage image);
// @optional -(void)imagePickerDidCancel:(MCImagePicker *)imagePicker;
[Export("imagePickerDidCancel:")]
void Canceled(MCImagePicker imagePicker);
}
// @interface MCImagePicker : NSObject
[BaseType(typeof(NSObject),
Delegates = new string[] { "WeakDelegate" },
Events = new Type[] { typeof(MCImagePickerDelegate) })]
interface MCImagePicker
{
[Wrap("WeakDelegate")]
MCImagePickerDelegate Delegate { get; set; }
// @property (nonatomic) id<MCImagePickerDelegate> delegate;
[NullAllowed, Export("delegate", ArgumentSemantic.Assign)]
NSObject WeakDelegate { get; set; }
// -(void)showFromController:(UIViewController *)controller completion:(void(^)(void))completion;
[Export("showFromController:completion:")]
[Async]
void ShowFromController(UIViewController controller, Action completion);
// -(void)dismiss:(void(^)(void))completion;
[Export("dismiss:")]
[Async]
void Dismiss(Action completion);
}
注意:这仅适用于Canceled方法。正确调用ImagePicked方法,没有任何异常。
如果我查看生成的代码,则会生成Canceled方法。以下是生成代码的摘录:
[Protocol]
[Register("MCImagePickerDelegate", false)]
[Model]
public unsafe partial class MCImagePickerDelegate : NSObject, IMCImagePickerDelegate {
[Export ("imagePickerDidCancel:")]
[CompilerGenerated]
public virtual void Canceled (MCImagePicker imagePicker)
{
throw new You_Should_Not_Call_base_In_This_Method ();
}
}
#pragma warning disable 672
[Register]
sealed class _MCImagePickerDelegate : MCImagePickerDelegate {
public _MCImagePickerDelegate () { IsDirectBinding = false; }
internal EventHandler canceled;
[Preserve (Conditional = true)]
public override void Canceled (MCImagePicker imagePicker)
{
EventHandler handler = canceled;
if (handler != null){
handler (imagePicker, EventArgs.Empty);
}
}
答案 0 :(得分:1)
回答我自己的问题,因为我找到了解决办法。
这似乎发生了,因为我在委托中的另一个方法上有EventArgs
。如果我更新了绑定,那么Canceled
方法也有EventArgs
:
// @optional -(void)imagePickerDidCancel:(MCImagePicker *)imagePicker;
[Export("imagePickerDidCancel:"), EventArgs("ImagePicked")]
void Canceled(MCImagePicker imagePicker);
重建 解决方案后,此方法有效。请注意,重建步骤是必要的。
我不确定是什么变化以及如何解决这个问题,因为生成的代码完全相同(我对它进行了分析)。但它对我有用。
<强>更新强>
您只需清理并重建解决方案即可。这就是所需要的。忽略添加EventArgs
属性的事情。似乎Xamarin在构建过程中有一个错误。