Xamarin 绑定事件 WeakDelegate 问题
Xamarin binding Events WeakDelegate issue
我有关注
iOS代码:
@protocol TestDelegate
- (void)onSuccess:(NSString*)token;
@end
@interface Utility : NSObject
@property (nullable, weak, getter = getTestDelegate, setter = setTestDelegate:) id<TestDelegate> delegate;
@end
Sharpie 生成了带有事件映射委托的代码:
[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; }
}
当我仅附加到事件而不使用委托 属性 时,不会触发事件。
如果我错了请纠正我,但是当仅使用事件时,没有任何内容是指内部创建的 _XDelegate,因此它将立即被垃圾收集。
在我的例子中,我必须在附加到事件后存储内部 _XDelegate 才能使它们工作。
public class TestClass
{
private readonly Delegate _del;
public TestClass()
{
iOS.SingletonInstance().OnSuccess += HandleOnSuccess;
_del = iOS.SingletonInstance().Delegate; //store Delegate instance to make events work
}
}
问题出在其他地方 - SingletonInstance() 是一种方法而不是 属性,因此没有任何内容指向返回的对象,因此它正在被 GC。
我有关注
iOS代码:
@protocol TestDelegate
- (void)onSuccess:(NSString*)token;
@end
@interface Utility : NSObject
@property (nullable, weak, getter = getTestDelegate, setter = setTestDelegate:) id<TestDelegate> delegate;
@end
Sharpie 生成了带有事件映射委托的代码:
[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; }
}
当我仅附加到事件而不使用委托 属性 时,不会触发事件。
如果我错了请纠正我,但是当仅使用事件时,没有任何内容是指内部创建的 _XDelegate,因此它将立即被垃圾收集。
在我的例子中,我必须在附加到事件后存储内部 _XDelegate 才能使它们工作。
public class TestClass
{
private readonly Delegate _del;
public TestClass()
{
iOS.SingletonInstance().OnSuccess += HandleOnSuccess;
_del = iOS.SingletonInstance().Delegate; //store Delegate instance to make events work
}
}
问题出在其他地方 - SingletonInstance() 是一种方法而不是 属性,因此没有任何内容指向返回的对象,因此它正在被 GC。