mono touch:Displaying Images

本文详细介绍了在Xamarin iOS应用中添加和显示图片的步骤,包括如何选择正确的Build Action,以及如何在代码中加载和展示图片。通过使用Xamarin Studio的资源目录,可以更方便地管理应用中的图片资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Adding images to your app requires two steps: first, add the images to your project; then add controls and code to display them on a screen. Refer to the Working with Images article for more detailed coverage of image handling in Xamarin.iOS.

Adding Images to Your App

Images can be added to any folder in your Xamarin Studio solution, and if the Build Action is set to Content then the file will be included with your app and can be displayed.

Xamarin Studio also supports a special directory called Resources that can also contain image files. Files in the Resources folder should have the Build Action set to BundleResource.

This screenshot shows the Build Action options when a file is right-clicked:

 

Xamarin Studio will typically choose the correct Build Action automatically but you should be aware of these settings, especially if you move files around in your project.

Adding An Image File

To add an image file to your project, first right-click the project and choose Add Files...

Select the image (or images) you wish to include in the standard file dialog. The default build action for images will be BundleResource – don’t override this value unless you have a specific reason.

The image will be added to your project and available to be loaded and displayed in code. This screenshot shows an image added to an iOS application project:

What is the Resources Directory?

Files placed in the Resources directory are treated differently from regular files – the contents of the Resources folder are copied to the root of the application and can be referenced from there in your code. This can be useful for many reasons:

  • Storing the images configured in the application’s properties, such as the default start-up images and application icons.
  • Storing other images and files separately from the code, so they’re easier to manage (subdirectories are preserved when the Resources directory contents are copied).

The Resources directory is especially useful in a Library project, since the code can assume that those images will be copied into the root of the consuming application, making it easier to write shared code libraries that require image, sound, video, XML or other files.

The Resources directory must be so named, and all files should have the Build Action set to BundleResource. This screenshot shows the image file blocks.jpg with the correct build action set:

Because blocks.jpg is located in the root of the Resources directory it will be available at runtime in the application bundle’s root. To display this image in an ImageView control use the following code:

imageview1.Image = UIImage.FromBundle ("blocks.jpg");

If we had placed the image in /Resources/Pics/blocks.jpg then the code would include the Pics folder in the path:

imageview1.Image = UIImage.FromBundle ("Pics/blocks.jpg");

Resource file references never need to include the Resources folder.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值