Monotouch 使用 using() 块创建 UIView
当我第一次开始使用 monotouch 时,我发现了一个页面,其中包含一些用于简单和频繁任务的代码示例...但在其中一些代码示例中,我发现了一些类似这样的内容:
var imageRect = new RectangleF(0f, 0f, 320f, 109f);
using (var myImage = new UIImageView(imageRect))
{
myImage.Image = UIImage.FromFile("myImage.png");
myImage.Opaque = true;
view.AddSubview(myImage);
}
UIImageView 是在 using() 块内创建的。
我是一名 .Net 开发人员,我知道 using() 的作用,但我不明白为什么在这个示例中使用它。 所以我的问题是,这是否是创建视图的最佳方法,这种方法与不使用 using() 块创建视图有什么区别(如果有)。
When i first started using monotouch i found a page with some code samples for simple and frequent tasks... but on some of those code samples i found some things like this one:
var imageRect = new RectangleF(0f, 0f, 320f, 109f);
using (var myImage = new UIImageView(imageRect))
{
myImage.Image = UIImage.FromFile("myImage.png");
myImage.Opaque = true;
view.AddSubview(myImage);
}
The UIImageView is created inside a using() block.
I'm a .Net developer and i know what a using() does, but i dont understand why is it used on this example.
So my question is if this is the best way of creating views, and what are the differences (if any) of this aproach and creating views without the using() block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Monotouch 文档,
NSObject
的 C# 版本实现了 < code>IDisposable 以便确保确定性销毁。可能,using
块是为了确保UIImageView
在从view
中删除后被完全销毁。According to the Monotouch documentation, the C# version of
NSObject
implementsIDisposable
so that deterministic destruction is ensured. Likely, theusing
block is to ensure that theUIImageView
is destroyed entirely once it's removed from theview