Monotouch 使用 using() 块创建 UIView

发布于 2024-09-05 07:46:48 字数 483 浏览 1 评论 0原文

当我第一次开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2024-09-12 07:46:48

根据 Monotouch 文档NSObject 的 C# 版本实现了 < code>IDisposable 以便确保确定性销毁。可能,using 块是为了确保 UIImageView 在从 view 中删除后被完全销毁。

According to the Monotouch documentation, the C# version of NSObject implements IDisposable so that deterministic destruction is ensured. Likely, the using block is to ensure that the UIImageView is destroyed entirely once it's removed from the view

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文