TheGejr
TheGejr

Reputation: 47

C# Xamarin load image from url

I am trying to load an image from a url string. Below code is an array of items, where Photo is what loads the image. Normally I included the image from my hard disk and loaded that image in by Photo = "image_name.jpg"

This is the first code:

public MainPageViewModel()
{        
    items.Add(new CardStackView.Item() { Name = "Title 1", Photo = " xxx ", Description = "Desc 1" });
    items.Add(new CardStackView.Item() { Name = "Title 2", Photo = " xxx ", Description = "Desc 2" });
    items.Add(new CardStackView.Item() { Name = "Title 3", Photo = " xxx ", Description = "Desc 3" });
    items.Add(new CardStackView.Item() { Name = "Title 4", Photo = " xxx ", Description = "Desc 4" });
    items.Add(new CardStackView.Item() { Name = "Title 5", Photo = " xxx ", Description = "Desc 5" });
    items.Add(new CardStackView.Item() { Name = "Title 6", Photo = " xxx ", Description = "Desc 6" });
}

I want xxx to be my url example https://i.vimeocdn.com/portrait/58832_300x300.

This piece of code is where I display the image:

Photo = new Image()
{
    InputTransparent = true,
    Aspect = Aspect.Fill,
    Scale = 0.95
};

view.Children.Add(Photo,
                  Constraint.RelativeToParent((parent) => { double w = parent.Width * 1; return ((parent.Width - w) / 2); }),
                  Constraint.Constant(10),
                  Constraint.RelativeToParent((parent) => { return parent.Width; }),
                  Constraint.RelativeToParent((parent) => { return (parent.Height* 0.80); }));  

Please note that these two pieces of code is not in the same file, but in two separate files.

I hope this isn't a duplicate, because I have not been able to find any solution to my problem.

Upvotes: 0

Views: 3541

Answers (2)

Mohamad Mahmoud Darwish
Mohamad Mahmoud Darwish

Reputation: 4175

I recommend you to use Binding,Basically you need only to set value for the Source. try the following:

  <Image Source="{Binding ImageUrl}" WidthRequest="80" HorizontalOptions="Start" VerticalOptions="Start" />

Upvotes: 0

SuavePirate
SuavePirate

Reputation: 1513

You need to set the Source of your Image Element:

Photo = new Image()
{
    InputTransparent = true,
    Aspect = Aspect.Fill,
    Scale = 0.95,
    Source = "https://your_image_url.com/the_image.png"
};

Upvotes: 1

Related Questions