简体   繁体   English

将 Swift 中的协议/委托映射到 C#

[英]Mapping protocol/delegates in Swift to c#

I'm trying to understand the usage of protocol and delegates in Swift and their equivalence in c# for Xamarin.iOS.我正在尝试了解 Swift 中协议和委托的用法以及它们在 C# 中的等效性 Xamarin.iOS。

Microsoft has a decent documentation about that here but for me it lacks the swift or objectiveC counterpart of c# code to allow me to understand what to do.微软在这里有一个不错的文档,但对我来说,它缺乏 c# 代码的 swift 或objectiveC 对应物,让我了解该怎么做。

I'm in the process of porting an existing Swift gist to c# Xamarin iOS.我正在将现有的 Swift 要点移植到 c# Xamarin iOS。

I have this protocol:我有这个协议:

protocol GridLayoutDelegate: class {
    func scaleForItem(inCollectionView collectionView: UICollectionView, withLayout layout: UICollectionViewLayout, atIndexPath indexPath: IndexPath) -> UInt
    func itemFlexibleDimension(inCollectionView collectionView: UICollectionView, withLayout layout: UICollectionViewLayout, fixedDimension: CGFloat) -> CGFloat
    func headerFlexibleDimension(inCollectionView collectionView: UICollectionView, withLayout layout: UICollectionViewLayout, fixedDimension: CGFloat) -> CGFloat
}

That I have converted to:我已转换为:

public interface IGridLayoutDelegate
{
    uint ScaleForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath);
    nfloat ItemFlexibleDimension(UICollectionView collectionView, UICollectionViewLayout layout, nfloat fixedDimension);
    nfloat HeaderFlexibleDimension(UICollectionView collectionView, UICollectionViewLayout layout, nfloat fixedDimension);
} 

So far so good.到现在为止还挺好。

Later in the code I have this (for now) strange declaration:稍后在代码中我有这个(现在)奇怪的声明:

weak var delegate: GridLayoutDelegate?

What concerns me is that it is never assigned later in the swift code.我担心的是它永远不会在 swift 代码中稍后分配。

For now I have my c# class declared as:现在我将我的 c# 类声明为:

public class GridLayout : UICollectionViewLayout, IGridLayoutDelegate
{
...
}

What would be the corresponding code in c#? c#中相应的代码是什么?

Swift language uses Automatic Reference Counting , so it needs weak var to deal with memory leaks, caused by circular references. Swift语言使用自动引用计数,因此它需要weak var来处理由循环引用引起的内存泄漏。

As opposing, C# uses Garbage Collector, which handles circular references by itself without extra manual effort (more detailed here: Garbage collector and circular reference . This works when both objects in the circular reference have similar lifetime, if one of the objects outlives another without unsubscribing there might pop up Lapsed listener problem )相反, C#使用垃圾收集器,它自己处理循环引用而无需额外的手动工作(更详细的在这里: 垃圾收集器和循环引用。当循环引用中的两个对象具有相似的生命周期时,如果其中一个对象比另一个对象寿命更长,则此方法有效取消订阅可能会弹出Lapsed listener 问题

So I think in C# you just need:所以我认为在C#你只需要:

public class GridLayout : UICollectionViewLayout, IGridLayoutDelegate
{
    private IGridLayoutDelegate @delegate;

I'm not familiar with Xamarin , so I cannot say for sure how @delegate should be initialized.我不熟悉Xamarin ,所以我不能确定应该如何初始化@delegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM