本文介绍了IBOutlets 在 Xamarin.iOS 中私有的原因是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,生成的designer.cs 属性是私有的(至少无需手动调整生成的代码).这使得针对 UITableViewCell 之类的东西进行编码的感觉与我在 Objective-C 中进行编码的感觉大不相同.

The generated designer.cs properties are private by default (at least without manual tweaking of generated code). This makes coding against something like a UITableViewCell feel much different than if I were doing this in Objective-C.

就 UIxxxViewCells 而言,最流行的方式是让 UIxxxViewDataSource 填充 IBOutlet 属性,并且该单元格应该只负责与绘制/渲染视图相关的任何事情,至少据我所知.

The popular way in the case of UIxxxViewCells, at least from what I can tell, is for the UIxxxViewDataSource to populate the IBOutlet properties, and that the cell should only be responsible for anything related to drawing/rendering the view.

使用 Xamarin.iOS,我们无法从数据源访问这些属性,而是需要提供额外的 setter 方法来填充单元格.这样,单元格负责设置自己的属性.

With Xamarin.iOS, we are unable to access these properties from the data source, and instead are required to provide additional setter methods to populate the cell. In this way, the cell is responsible for setting it's own properties.

这只是做事的.NET 方式"吗?

Is this just "The .NET way" of doing things?

推荐答案

这样我们就不会在默认情况下破坏封装.

It's so that we don't break encapsulation by default.

outlet 属于它们所在的对象,应该可以选择是否可以从外部修改.它们是属性这一事实是 Xamarin.iOS 插座系统的实现细节 - 您应该将它们视为私有字段.

The outlets belong to the object they're on, it should be able to choose whether they're able to be modified from the outside. The fact that they're properties is an implementation detail of the Xamarin.iOS outlets system - you should think of them as private fields.

如果您希望公开它们,您可以创建这样做的属性 - 最好是只读的.

If you wish to expose them, you can create properties that do so - preferably read-only.

这篇关于IBOutlets 在 Xamarin.iOS 中私有的原因是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:43