Avacay
Avacay

Reputation: 163

dynamically add images to view

I'm trying to make a carousel in my Xamarin application. Here I need to add navigation "dots" below my carousel, in order to intuitively show the user which slide is shown in the carousel.

The Carousel is initialized from a List of objects, which may vary in size, which naturally means that I have to place a corresponding amount of dots at runtime.

Currently, I have bounded a ViewModel to my View, which updates the content of the carousel through a PropertyChangedEventHandler. The updates are currently controlled with a timer.

To summarize, I want to know, how I can add <Image> elements via code-behind in a .xaml file. Furthermore, I want to understand how I can identify them uniquely so that I can access them and change the image, based on which page is shown in the carousel.

Upvotes: 2

Views: 892

Answers (1)

Jason
Jason

Reputation: 89102

to add an Image (or any element) to a View at runtime

// XAML
<StackLayout x:Name="stack">
  ...
</StackLayout>

// in code behind
stack.Children.Add(new Image() { ... });

Upvotes: 2

Related Questions