如何将StrongDelegate ObjC代码移植到C#MonoTouch?

时间:2021-02-28 15:05:49

I'm porting the Official Apple Multipeer WiTap sample to Xamarin and C#, however I'm not sure how to port this method within TapViewController.

我正在将官方Apple Multipeer WiTap示例移植到Xamarin和C#,但是我不确定如何在TapViewController中移植此方法。

It contains all the things I'm not sure how to port... namely

它包含了我不确定如何移植的所有东西......即

  1. A delegate that is expressed only in code, and has no strong delegate in the Xamarin framework. (this is probably easy, just a concept I'm missing)
  2. 仅在代码中表示的委托,在Xamarin框架中没有强委托。 (这可能很简单,只是我缺少的一个概念)
  3. What the heck is the id here, and how do I declare/use it?
  4. 这里的ID到底是什么,我该如何声明/使用它?
  5. How do I declare self.delegate;
  6. 我如何声明self.delegate;

code:

码:

- (IBAction)closeButtonAction:(id)sender
{
    id <TapViewControllerDelegate>  strongDelegate;

    #pragma unused(sender)
    strongDelegate = self.delegate;
    if ([strongDelegate respondsToSelector:@selector(tapViewControllerDidClose:)]) {
        [strongDelegate tapViewControllerDidClose:self];
    }
}

Here is a link to my code, where the port is in progress

这是我的代码的链接,端口正在进行中

1 个解决方案

#1


1  

Are you attempting to port Objective-C code to C# without knowing the former?

您是否在不知道前者的情况下尝试将Objective-C代码移植到C#?

  • id is Obj-C's "any object", while id<protocolName> is any object which implements the specified protocol. An Obj-C "delegate" is just an object which implements a given protocol, it is the way it is used that makes it a delegate. Objc-C protocols and C# interfaces are matching concepts. So strongDelegate is a variable whose type is a C# interface (which you've presumably translated from the Obj-C protocol).
  • id是Obj-C的“任何对象”,而id 是实现指定协议的任何对象。 Obj-C“委托”只是一个实现给定协议的对象,它的使用方式使它成为委托。 Objc-C协议和C#接口是匹配的概念。因此,strongDelegate是一个变量,其类型是C#接口(您可能已经从Obj-C协议转换)。
  • Declaring self.delegate: it's an Objc-C property reference with the same (or compatible) type as strongDelegate. C# has properties.
  • 声明self.delegate:它是一个Objc-C属性引用,具有与strongDelegate相同(或兼容)的类型。 C#有属性。
  • The if test in Obj-C is determine whether the referenced object implements the specified method and if so invokes it. Obj-C protocols allow optional methods, i.e. methods objects implementing the protocol need not implement. C# interfaces have no direct equivalent. In your translation of the Obj-C protocol to a C# interface you either made all optional methods non-optional, or you did something else. Translate the if to match whatever you did.
  • Obj-C中的if测试确定引用的对象是否实现了指定的方法,如果是,则调用它。 Obj-C协议允许可选方法,即实现协议的方法对象不需要实现。 C#接口没有直接的等价物。在将Obj-C协议转换为C#接口时,您要么将所有可选方法都设置为非可选方法,要么您还做了其他方法。翻译if以匹配您所做的任何事情。

HTH

HTH

#1


1  

Are you attempting to port Objective-C code to C# without knowing the former?

您是否在不知道前者的情况下尝试将Objective-C代码移植到C#?

  • id is Obj-C's "any object", while id<protocolName> is any object which implements the specified protocol. An Obj-C "delegate" is just an object which implements a given protocol, it is the way it is used that makes it a delegate. Objc-C protocols and C# interfaces are matching concepts. So strongDelegate is a variable whose type is a C# interface (which you've presumably translated from the Obj-C protocol).
  • id是Obj-C的“任何对象”,而id 是实现指定协议的任何对象。 Obj-C“委托”只是一个实现给定协议的对象,它的使用方式使它成为委托。 Objc-C协议和C#接口是匹配的概念。因此,strongDelegate是一个变量,其类型是C#接口(您可能已经从Obj-C协议转换)。
  • Declaring self.delegate: it's an Objc-C property reference with the same (or compatible) type as strongDelegate. C# has properties.
  • 声明self.delegate:它是一个Objc-C属性引用,具有与strongDelegate相同(或兼容)的类型。 C#有属性。
  • The if test in Obj-C is determine whether the referenced object implements the specified method and if so invokes it. Obj-C protocols allow optional methods, i.e. methods objects implementing the protocol need not implement. C# interfaces have no direct equivalent. In your translation of the Obj-C protocol to a C# interface you either made all optional methods non-optional, or you did something else. Translate the if to match whatever you did.
  • Obj-C中的if测试确定引用的对象是否实现了指定的方法,如果是,则调用它。 Obj-C协议允许可选方法,即实现协议的方法对象不需要实现。 C#接口没有直接的等价物。在将Obj-C协议转换为C#接口时,您要么将所有可选方法都设置为非可选方法,要么您还做了其他方法。翻译if以匹配您所做的任何事情。

HTH

HTH