Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show image from URL with Xamarin.Forms

I am using this code to show image from an URL

.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LandAHand.VolunteerView">
    <ContentPage.Content>
         <AbsoluteLayout BackgroundColor="Maroon">
             <Image x:Name="backgroundImage" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" />
         </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

.cs

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace LandAHand
{
    public partial class VolunteerView : ContentPage
    {
        public VolunteerView()
        {
            InitializeComponent();
            backgroundImage.Source = new UriImageSource
            {
                Uri = new Uri("https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg"),
                CachingEnabled = true,
                CacheValidity = new TimeSpan(5, 0, 0, 0)
            };
        }
    }
}

This code is successfully working with iOS but it is not working with Android.

like image 659
Kishore Suthar Avatar asked Aug 12 '16 05:08

Kishore Suthar


People also ask

How do you bind an image source in Xamarin forms?

In Xamarin. Forms SfAvatarView, you can set the key for ImageSource by using ResourceDictionary and bind image from ViewModel to ImageSource. An ImageSource instance, can be either File, Uri or Resource, which sets the image to display.

How to use image in Xamarin?

Go to Start -> New Project-> select Cross-Platform (under Visual C# -> Blank App (Xamarin. Forms Portable)-> Give a suitable name to your app (XamFormImg) -> OK. Now, create project “XamFormImg_Droid”. Choose the target and minimum platform versions for your Universal Windows Project.

How do I use SVG images in Xamarin forms?

Forms does not support SVG files out of the box, they can be easily rendered using the Xamarin. Forms. Svg NuGet package. I created a value converter that generates the correct image source for each image type: SvgImageSource from this NuGet package for SVG URLs and the default ImageSource for all other URLs.


1 Answers

Well you can do this thing easier with your Xaml just make your xaml like this

<Image x:Name="backgroundImage" Source="https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1"   AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>

And remove the code related in the code behind. The cashing is enabled by default for 24 hours

like image 58
Ahmad ElMadi Avatar answered Oct 11 '22 22:10

Ahmad ElMadi