7.1 Embedding images from different sources
Xamarin.Forms allows you to embed images from three different sources: local images (stored in the platform-specific file structure), embedded images (stored in the assembly as an embedded resource) and remote images (downloaded from the web).
Local images
To add a local image to the cross-platform app, you can simply drag the image file from your system’s file manager into the Xamarin.Studio “Resources” folder on iOS and “Resources/drawable” folder on Android, respectively. The build action should be automatically set to “BundleResource” and “AndroidResource”. Then you can create an Image as follows.
Content = new Image {
Source = ImageSource.FromFile("xamarin.png"),
}
,Embedded imagesAn embedded image can be stored in the shared project. Just make sure to set the build action to “EmbeddedResource” and assign a Resource ID, “xamarin” in this example. This ID is then used to refer to the image:
Content = new Image {
Source = ImageSource.FromResource("xamarin"),
}
,Remote imagesLast but not least, we can refer to an online resource. Obviously, this method might cause some delay, which affects the user experience. Or even worse: They might be unavailable due to missing internet connection. Thus, we should download images only if they are unavailable at compile time.
Content = new Image {
Source = ImageSource.FromUri(new Uri("http://tinyurl.com/nmd85s3"))
}
,The resulting screen is identical for all image sources.